
* DATA ANALYSIS FILE FOR "ECONOMIC SHOCKS AND CONFLICTS"
* WINTER 2014
* SAMUEL BAZZI AND CHRISTOPHER BLATTMAN

*************************************************************************************************
***COPY THE NAME OF YOUR DIRECTORY INTO THE COMMAND BELOW
cd ""

adopath + Code/
*************************************************************************************************

// NOTES REGARDING REPLICATION
* p-values for disaggregated price shock regressions must be extracted manually from .dta files 
*	due to unresolved coding error in outreg2 handling of added stats in local macros.

* clogit has considerable difficulty converging and/or returning variance estimates, 
*	and also yield non-identical esitmates on consecutive runs of the data, 
*	especially in non-convergent regressions but also in convergent ones. 
*	We are in ongiong discusion with Stata tech folks to resolve the issue.


// SET UP 
clear
cap clear matrix
cap log c
set more off 
macro drop _all

set seed 12345
set sortseed 12345
global maxiter 100

set matsize 800 
set mem 1100m 
set scheme s1mono


* LOAD DATA
use Data/BAZZI_BLATTMAN_FINAL, clear


* LOAD TESTING AND OUTPUT PROGRAMS
do Code/outregFtestProgs


* DEPENDENT VARIABLE MACROS
global incidence any_prio war_prio_bb war_prio_mss war_fl war_samb war_cow
global onset onset_any_prio onset_war_prio_bb onset_war_prio_mss onset_war_fl onset_war_samb onset_war_cow 
global FLonset fl_onset_any_prio fl_onset_war_prio_bb fl_onset_war_prio_mss fl_onset_war_fl fl_onset_war_samb fl_onset_war_cow 
global end end_any_prio end_war_prio_bb end_war_prio_mss end_war_fl end_war_samb end_war_cow
global instab exit_irregular2 couppt 


* MISCELLANEOUS MACROS
global finite
global IF if (state_exist==1 | state_exist_cow==1)
global robustY onset_any_prio onset_war_prio_bb any_prio war_prio_bb exit_irregular2 
global clu cow_id 
global addClust e(N_clust)
tsset cow_id year

* TIME, COUNTRY, AND TREND FES
cap program drop prepFE
program define prepFE

	tsset cow_id year
	sort year
	qui tab year, gen(Iy)
	gen t = year - 1957

	egen ccid = group(cow_id)
	qui tab ccid,gen(Iccid)

	qui su ccid
	forvalues i=1/`=r(max)' {
		gen It`i' = Iccid`i'*t
	}

	tsset cow_id year
	
	global year Iy*
	global country Iccid*
	global country_year It*
	
end


*IMPORT PRICE SHOCKS
cap program drop prepImport
program define prepImport

	global import agg_pshock_sum agg_pshock_sum1 agg_pshock_sum2

	global importRegion $import mena_shock* ssa_shock* asia_shock* americas_shock*
	gen mena = mideast + nafrica
	foreach v in mena ssa asia americas {
		gen `v'_shock = `v' * agg_pshock_sum
		gen `v'_shock1 = `v' * agg_pshock_sum1
		gen `v'_shock2 = `v' * agg_pshock_sum2
	}

	global importCountry $import importShockCountry* importShock1Country* importShock2Country*
	qui su ccid
	forvalues i=1/`=r(max)' {
		gen importShockCountry`i' = Iccid`i' * agg_pshock_sum
		gen importShock1Country`i' = Iccid`i' * agg_pshock_sum1
		gen importShock2Country`i' = Iccid`i' * agg_pshock_sum2
	}

end


* GET LAGGED SHOCKS LINED UP POST-NORMALIZATION
cap program drop prepRain
program define prepRain

	tsset

	egen lnRain = std(ln(wpre))
	egen lnRain_L1 = std(ln(wpre_L1))
	egen lnRain_L2 = std(ln(wpre_L2))
	egen lnRain_L3 = std(ln(wpre_L3))

	egen g_lnRain = std(lnRain - lnRain_L1)
	egen g_lnRain_L1 = std(lnRain_L1 - lnRain_L2)
	egen g_lnRain_L2 = std(lnRain_L2 - lnRain_L3)

end

* SET LAGGING STRUCTURE SO NORMALIZATIONS SAME ACROSS LAGS
cap program drop prepLag
program define prepLag
	foreach v in pshock_npi_p pshock_npi pshock_npf80_p pshock_npf80 pshock_annual_npi_p pshock_annual_npi pshock_annual_npf80_p pshock_perennial_npi_p pshock_perennial_npi pshock_perennial_npf80_p pshock_extract_npi_p pshock_extract_npi pshock_extract_npf80_p z_rpgr_npi z_rpgr_annual_npi z_rpgr_perennial_npi z_rpgr_extract_npi {
		replace `v'1 = l.`v'
		replace `v'2 = l2.`v'
		if substr("`v'",1,1)!="z" {
			replace i`v'1 = l.i`v'
			replace i`v'2 = l2.i`v'
		}
	}
	
	foreach v in pshock_npi pshock_annual_npi pshock_perennial_npi pshock_extract_npi {
		replace `v'_p1_03 = l.`v'_p_03
		replace `v'_p2_03 = l2.`v'_p_03
		replace `v'_p1_2 = l.`v'_p_2
		replace `v'_p2_2 = l2.`v'_p_2
		
		replace i`v'_p1_03 = l.i`v'_p_03
		replace i`v'_p2_03 = l2.i`v'_p_03
		replace i`v'_p1_2 = l.i`v'_p_2
		replace i`v'_p2_2 = l2.i`v'_p_2
	} 
end


* GET DATA READY
prepFE
prepImport
prepRain
prepLag


***************************************************************************************************************************************
* MAIN PAPER FIGURES & TABLES
***************************************************************************************************************************************


*---------------------------------------------------------------------------------
// TABLE 1: SUMMARY STATISTICS
*---------------------------------------------------------------------------------

*--> selecting base sample
qui xtreg war_cow pshock_npi_p pshock_npi_p1 pshock_npi_p2 $year $country_year $IF, fe
gen BASEsmp = e(sample)

log using TablesFigures/summstats,text replace
su $instab if BASEsmp
su $onset $end $incidence if BASEsmp

su pshock_npi_p pshock_annual_npi_p pshock_perennial_npi_p pshock_extract_npi_p lw primexpbase2gdp primexpwt2gdp primexp2totalexp XGDP ///
	gdpg_wdi gdpg_pwt gdppcg_pwt gdppcg_wdi gdplng_wdi gdplng_pwt gdppclng_pwt gdppclng_wdi lng_govconsexp lng_taxrev lng_rev partial_autoc partial_democ partial_democ_fac full_democ if BASEsmp
su gini if year==2000, d
log c


*---------------------------------------------------------------------------------
// TABLES 2 and 4: BASELINE AGGREGATE SHOCKS SPECIFICATION
*---------------------------------------------------------------------------------

global agg pshock_npi_p pshock_npi_p1 pshock_npi_p2
global X $agg
global aggsum pshock_npi_p+pshock_npi_p1+pshock_npi_p2
global Xsum $aggsum

cap mat drop pvalAGGSUM
global table TablesFigures/BASEAGG-TABLES-2AND4
cap erase $table.xml
cap erase $table.txt
foreach y in $onset $instab $end {
	qui xtreg `y' $X $year $country_year $IF, cl($clu) fe
	outFlincom
}


* ADDING IMPORT PRICE SHOCKS
cap mat drop pvalAGGSUM
global table TablesFigures/BASEAGG-TABLES-2AND4-IMPORTSHOCK
cap erase $table.xml
cap erase $table.txt
foreach y in $onset $instab $end {
	global IMPX $importCountry
	qui xtreg `y' $X $IMPX $year $country_year $IF, cl($clu) fe 
	outFlincomIMPORT		
}


*---------------------------------------------------------------------------------
// TABLES 3 and 5: BASELINE DISAGGREGATED SHOCKS SPECIFICATION
*---------------------------------------------------------------------------------

global annagg pshock_annual_npi_p pshock_annual_npi_p1 pshock_annual_npi_p2
global peragg pshock_perennial_npi_p pshock_perennial_npi_p1 pshock_perennial_npi_p2
global extagg pshock_extract_npi_p pshock_extract_npi_p1 pshock_extract_npi_p2
global X $annagg $peragg $extagg
global annaggsum pshock_annual_npi_p+pshock_annual_npi_p1+pshock_annual_npi_p2
global peraggsum pshock_perennial_npi_p+pshock_perennial_npi_p1+pshock_perennial_npi_p2
global extaggsum pshock_extract_npi_p+pshock_extract_npi_p1+pshock_extract_npi_p2

cap mat drop pvalANNUAL pvalPERENNIAL pvalEXTRACT
global table TablesFigures/BASEDISAGG-TABLES-3AND5
cap erase $table.xml
cap erase $table.txt
foreach y in $onset $instab $end {
	qui xtreg `y' $X $year $country_year $IF, cl($clu) fe
	outFdisaglincom
}

preserve
svmat pvalANNUAL 
svmat pvalPERENNIAL 
svmat pvalEXTRACT
keep if pvalANNUAL<.
keep pval*
save TablesFigures/pval-BASEDISAGG,replace
restore


* ADDING IMPORT PRICE SHOCKS
cap mat drop pvalANNUAL pvalPERENNIAL pvalEXTRACT
global table TablesFigures/BASEDISAGG-TABLES-3AND5-IMPORTSHOCK
cap erase $table.xml
cap erase $table.txt
foreach y in $onset $instab $end {	
	global IMPX $importCountry
	qui xtreg `y' $X $IMPX $year $country_year $IF, cl($clu) fe 
	outFdisaglincomIMPORT	
}

preserve
svmat pvalANNUAL 
svmat pvalPERENNIAL 
svmat pvalEXTRACT
keep if pvalANNUAL<.
keep pval*
save TablesFigures/pval-BASEDISAGG-IMPORTSHOCK,replace
restore


**************************************************************************************************
***********************************BASELINE (+ IMPORT SHOCKS)*************************************
**************************************************************************************************
	
global IMPX $importCountry	

*---------------------------------------------------------------------------------
// FIGURES 1 and 4: ALL DEP VARS
*---------------------------------------------------------------------------------
	
cap mat drop pvalANNUAL_END pvalPERENNIAL_END pvalEXTRACT_END pvalANNUAL_ON pvalPERENNIAL_ON pvalEXTRACT_ON
cap mat drop pvalANNUAL_END_LOG pvalPERENNIAL_END_LOG pvalEXTRACT_END_LOG pvalANNUAL_ON_LOG pvalPERENNIAL_ON_LOG pvalEXTRACT_ON_LOG
cap mat drop impactANNUAL_END_LOG impactPERENNIAL_END_LOG impactEXTRACT_END_LOG impactANNUAL_ON_LOG impactPERENNIAL_ON_LOG impactEXTRACT_ON
cap mat drop impactANNUAL_END impactPERENNIAL_END impactEXTRACT_END impactANNUAL_ON impactPERENNIAL_ON impactEXTRACT_ON
cap mat drop convergenceON_LOG convergenceEND_LOG

global annagg sA sA1 sA2
global peragg sP sP1 sP2
global extagg sE sE1 sE2
global X $annagg $peragg $extagg
global annaggsum sA+sA1+sA2
global peraggsum sP+sP1+sP2
global extaggsum sE+sE1+sE2


foreach y in $onset $end {

	dis "****************************`y'****************************"

	if substr("`y'",1,3)=="end" {
		local outF outFdisaglincomEND
		local matC convergenceEND_LOG 
	}
	else {
		local outF outFdisaglincomON
		local matC convergenceON_LOG 
	}

	* -----LPM----- *
	dis "=====================LPM====================="
	
	global est 

	* BASE
	cap drop sA sA1 sA2 sP sP1 sP2 sE sE1 sE2
	gen sA  = pshock_annual_npi_p
	gen sA1 = l.pshock_annual_npi_p
	gen sA2 = l2.pshock_annual_npi_p
	gen sP  = pshock_perennial_npi_p
	gen sP1 = l.pshock_perennial_npi_p
	gen sP2 = l2.pshock_perennial_npi_p
	gen sE  = pshock_extract_npi_p
	gen sE1 = l.pshock_extract_npi_p
	gen sE2 = l2.pshock_extract_npi_p
	qui xtreg `y' $X $year $country_year $IMPX $IF, cl($clu) fe 
	`outF'

	* no import shocks
	cap drop sA sA1 sA2 sP sP1 sP2 sE sE1 sE2
	gen sA  = pshock_annual_npi_p
	gen sA1 = l.pshock_annual_npi_p
	gen sA2 = l2.pshock_annual_npi_p
	gen sP  = pshock_perennial_npi_p
	gen sP1 = l.pshock_perennial_npi_p
	gen sP2 = l2.pshock_perennial_npi_p
	gen sE  = pshock_extract_npi_p
	gen sE1 = l.pshock_extract_npi_p
	gen sE2 = l2.pshock_extract_npi_p
	qui xtreg `y' $X $year $country_year $IF, cl($clu) fe 
	`outF'	
	
	* no x/gdp adjustment
	cap drop sA sA1 sA2 sP sP1 sP2 sE sE1 sE2
	gen sA  = z_rpgr_annual_npi_p
	gen sA1 = l.z_rpgr_annual_npi_p
	gen sA2 = l2.z_rpgr_annual_npi_p
	gen sP  = z_rpgr_perennial_npi_p
	gen sP1 = l.z_rpgr_perennial_npi_p
	gen sP2 = l2.z_rpgr_perennial_npi_p
	gen sE  = z_rpgr_extract_npi_p
	gen sE1 = l.z_rpgr_extract_npi_p
	gen sE2 = l2.z_rpgr_extract_npi_p
	qui xtreg `y' $X $year $country_year $IMPX $IF, cl($clu) fe 
	`outF'
	
	* w/ PRICEMAKERS
	cap drop sA sA1 sA2 sP sP1 sP2 sE sE1 sE2
	gen sA  = pshock_annual_npi
	gen sA1 = l.pshock_annual_npi
	gen sA2 = l2.pshock_annual_npi
	gen sP  = pshock_perennial_npi
	gen sP1 = l.pshock_perennial_npi
	gen sP2 = l2.pshock_perennial_npi
	gen sE  = pshock_extract_npi
	gen sE1 = l.pshock_extract_npi
	gen sE2 = l2.pshock_extract_npi
	qui xtreg `y' $X $year $country_year $IMPX $IF, cl($clu) fe 
	`outF'
	
	* 3% pricemaker cutoff
	cap drop sA sA1 sA2 sP sP1 sP2 sE sE1 sE2
	gen sA  = pshock_annual_npi_p_03
	gen sA1 = l.pshock_annual_npi_p_03
	gen sA2 = l2.pshock_annual_npi_p_03
	gen sP  = pshock_perennial_npi_p_03
	gen sP1 = l.pshock_perennial_npi_p_03
	gen sP2 = l2.pshock_perennial_npi_p_03
	gen sE  = pshock_extract_npi_p_03
	gen sE1 = l.pshock_extract_npi_p_03
	gen sE2 = l2.pshock_extract_npi_p_03
	qui xtreg `y' $X $year $country_year $IMPX $IF, cl($clu) fe 
	`outF'
	
	* 20% pricemaker cutoff
	cap drop sA sA1 sA2 sP sP1 sP2 sE sE1 sE2
	gen sA  = pshock_annual_npi_p_2
	gen sA1 = l.pshock_annual_npi_p_2
	gen sA2 = l2.pshock_annual_npi_p_2
	gen sP  = pshock_perennial_npi_p_2
	gen sP1 = l.pshock_perennial_npi_p_2
	gen sP2 = l2.pshock_perennial_npi_p_2
	gen sE  = pshock_extract_npi_p_2
	gen sE1 = l.pshock_extract_npi_p_2
	gen sE2 = l2.pshock_extract_npi_p_2
	qui xtreg `y' $X $year $country_year $IMPX $IF, cl($clu) fe
	`outF'	
	
	* FIXED WEIGHTS
	cap drop sA sA1 sA2 sP sP1 sP2 sE sE1 sE2
	gen sA  = pshock_annual_npf80_p
	gen sA1 = l.pshock_annual_npf80_p
	gen sA2 = l2.pshock_annual_npf80_p
	gen sP  = pshock_perennial_npf80_p
	gen sP1 = l.pshock_perennial_npf80_p
	gen sP2 = l2.pshock_perennial_npf80_p
	gen sE  = pshock_extract_npf80_p
	gen sE1 = l.pshock_extract_npf80_p
	gen sE2 = l2.pshock_extract_npf80_p
	qui xtreg `y' $X $year $country_year $IMPX $IF, cl($clu) fe 
	`outF'
	
	* CENSOR OUTLIERS
	cap drop sA sA1 sA2 sP sP1 sP2 sE sE1 sE2
	gen sA  = pshock_annual_npi_p
	gen sA1 = l.pshock_annual_npi_p
	gen sA2 = l2.pshock_annual_npi_p
	gen sP  = pshock_perennial_npi_p
	gen sP1 = l.pshock_perennial_npi_p
	gen sP2 = l2.pshock_perennial_npi_p
	gen sE  = pshock_extract_npi_p
	gen sE1 = l.pshock_extract_npi_p
	gen sE2 = l2.pshock_extract_npi_p
	cap drop keeper
	gen keeper = .
	foreach v in sA sP sE {
		qui su `v', det
		qui replace keeper = `v'>=r(p1) & `v'<=r(p99)
		qui su `v'1, det
		qui replace keeper = `v'1>=r(p1) & `v'1<=r(p99) & keeper==1
		qui su `v'2, det
		qui replace keeper = `v'2>=r(p1) & `v'2<=r(p99) & keeper==1
	}
	qui xtreg `y' $X $year $country_year $IMPX $IF & keeper==1, cl($clu) fe 
	`outF'
	
	* DROPPING COUNTRY-SPECIFIC TIME TRENDS
	qui xtreg `y' $X $year $IMPX $IF, cl($clu) fe
	`outF'	
	
	* EXCLUDING TIME FEs
	qui xtreg `y' $X $country_year $IMPX $IF, cl($clu) fe
	`outF'	
	
	* DROPPING COUNTRY FEs 
	qui reg `y' $X $year $country_year $IMPX $IF, cl($clu)
	`outF'		

	* -----CONDITIONAL LOGIT----- *
	dis "=====================LOGIT====================="

	global est _LOG
	
	* BASE
	cap drop sA sA1 sA2 sP sP1 sP2 sE sE1 sE2
	gen sA  = pshock_annual_npi_p
	gen sA1 = l.pshock_annual_npi_p
	gen sA2 = l2.pshock_annual_npi_p
	gen sP  = pshock_perennial_npi_p
	gen sP1 = l.pshock_perennial_npi_p
	gen sP2 = l2.pshock_perennial_npi_p
	gen sE  = pshock_extract_npi_p
	gen sE1 = l.pshock_extract_npi_p
	gen sE2 = l2.pshock_extract_npi_p	
	qui clogit `y' $X $year $IF, cl(cow_id) group(cow_id) iterate($maxiter) 
	matrix `matC' = (nullmat(`matC') \ e(converged))
	`outF'
	
	* no x/gdp adjustment
	cap drop sA sA1 sA2 sP sP1 sP2 sE sE1 sE2
	gen sA  = z_rpgr_annual_npi_p
	gen sA1 = l.z_rpgr_annual_npi_p
	gen sA2 = l2.z_rpgr_annual_npi_p
	gen sP  = z_rpgr_perennial_npi_p
	gen sP1 = l.z_rpgr_perennial_npi_p
	gen sP2 = l2.z_rpgr_perennial_npi_p
	gen sE  = z_rpgr_extract_npi_p
	gen sE1 = l.z_rpgr_extract_npi_p
	gen sE2 = l2.z_rpgr_extract_npi_p
	qui clogit `y' $X $year $IF, cl(cow_id) group(cow_id) iterate($maxiter) 
	matrix `matC' = (nullmat(`matC') \ e(converged))
	`outF'
	
	* w/ PRICEMAKERS
	cap drop sA sA1 sA2 sP sP1 sP2 sE sE1 sE2
	gen sA  = pshock_annual_npi
	gen sA1 = l.pshock_annual_npi
	gen sA2 = l2.pshock_annual_npi
	gen sP  = pshock_perennial_npi
	gen sP1 = l.pshock_perennial_npi
	gen sP2 = l2.pshock_perennial_npi
	gen sE  = pshock_extract_npi
	gen sE1 = l.pshock_extract_npi
	gen sE2 = l2.pshock_extract_npi
	qui clogit `y' $X $year $IF, cl(cow_id) group(cow_id) iterate($maxiter) 
	matrix `matC' = (nullmat(`matC') \ e(converged))
	`outF'
	
	* 3% pricemaker cutoff
	cap drop sA sA1 sA2 sP sP1 sP2 sE sE1 sE2
	gen sA  = pshock_annual_npi_p_03
	gen sA1 = l.pshock_annual_npi_p_03
	gen sA2 = l2.pshock_annual_npi_p_03
	gen sP  = pshock_perennial_npi_p_03
	gen sP1 = l.pshock_perennial_npi_p_03
	gen sP2 = l2.pshock_perennial_npi_p_03
	gen sE  = pshock_extract_npi_p_03
	gen sE1 = l.pshock_extract_npi_p_03
	gen sE2 = l2.pshock_extract_npi_p_03
	qui clogit `y' $X $year $IF, cl(cow_id) group(cow_id) iterate($maxiter) 
	matrix `matC' = (nullmat(`matC') \ e(converged))
	`outF'
	
	* 20% pricemaker cutoff
	cap drop sA sA1 sA2 sP sP1 sP2 sE sE1 sE2
	gen sA  = pshock_annual_npi_p_2
	gen sA1 = l.pshock_annual_npi_p_2
	gen sA2 = l2.pshock_annual_npi_p_2
	gen sP  = pshock_perennial_npi_p_2
	gen sP1 = l.pshock_perennial_npi_p_2
	gen sP2 = l2.pshock_perennial_npi_p_2
	gen sE  = pshock_extract_npi_p_2
	gen sE1 = l.pshock_extract_npi_p_2
	gen sE2 = l2.pshock_extract_npi_p_2
	qui clogit `y' $X $year $IF, cl(cow_id) group(cow_id) iterate($maxiter) 
	matrix `matC' = (nullmat(`matC') \ e(converged))
	`outF'
	
	* FIXED WEIGHTS
	cap drop sA sA1 sA2 sP sP1 sP2 sE sE1 sE2
	gen sA  = pshock_annual_npf80_p
	gen sA1 = l.pshock_annual_npf80_p
	gen sA2 = l2.pshock_annual_npf80_p
	gen sP  = pshock_perennial_npf80_p
	gen sP1 = l.pshock_perennial_npf80_p
	gen sP2 = l2.pshock_perennial_npf80_p
	gen sE  = pshock_extract_npf80_p
	gen sE1 = l.pshock_extract_npf80_p
	gen sE2 = l2.pshock_extract_npf80_p
	qui clogit `y' $X $year $IF, cl(cow_id) group(cow_id) iterate($maxiter) 
	matrix `matC' = (nullmat(`matC') \ e(converged))
	`outF'
	
	* CENSOR OUTLIERS
	cap drop sA sA1 sA2 sP sP1 sP2 sE sE1 sE2
	gen sA  = pshock_annual_npi_p
	gen sA1 = l.pshock_annual_npi_p
	gen sA2 = l2.pshock_annual_npi_p
	gen sP  = pshock_perennial_npi_p
	gen sP1 = l.pshock_perennial_npi_p
	gen sP2 = l2.pshock_perennial_npi_p
	gen sE  = pshock_extract_npi_p
	gen sE1 = l.pshock_extract_npi_p
	gen sE2 = l2.pshock_extract_npi_p
	cap drop keeper	
	gen keeper = .
	foreach v in sA sP sE {
		qui su `v', det
		qui replace keeper = `v'>=r(p1) & `v'<=r(p99)
		qui su `v'1, det
		qui replace keeper = `v'1>=r(p1) & `v'1<=r(p99) & keeper==1
		qui su `v'2, det
		qui replace keeper = `v'2>=r(p1) & `v'2<=r(p99) & keeper==1
	}
	qui clogit `y' $X $year $IF & keeper==1, cl(cow_id) group(cow_id) iterate($maxiter) 
	matrix `matC' = (nullmat(`matC') \ e(converged))
	`outF'

	* INCLUDING COUNTRY-SPECIFIC TIME TRENDS
	qui clogit `y' $X $year $country_year $IF, cl(cow_id) group(cow_id) iterate($maxiter) 
	matrix `matC' = (nullmat(`matC') \ e(converged))
	`outF'
	
	* EXCLUDING TIME FEs
	qui clogit `y' $X $IF, cl(cow_id) group(cow_id) iterate($maxiter) 
	matrix `matC' = (nullmat(`matC') \ e(converged))
	`outF'
	
	* DROPPING COUNTRY FEs (& COUNTRY-SPECIFIC TIME TRENDS)
	qui logit `y' $X $year $IF, cl(cow_id) iterate($maxiter) 
	matrix `matC' = (nullmat(`matC') \ e(converged))
	`outF'	

}


preserve
foreach v in ANNUAL PERENNIAL EXTRACT {
	foreach est in "" _LOG {
		svmat pval`v'_ON`est'
		svmat pval`v'_END`est'
		svmat impact`v'_ON`est'
		svmat impact`v'_END`est'
	}
}
svmat convergenceON_LOG
svmat convergenceEND_LOG
keep pval* impact* convergenceEND_LOG convergenceON_LOG
rensfix 1
gen baseline = _n==1
*forvalues j=1/6 {
*	replace baseline = 1 if _n==(1+`j'*10)
*}
keep if pvalANNUAL_ON<. | pvalANNUAL_END<. | pvalPERENNIAL_ON<. | pvalPERENNIAL_END<. | pvalEXTRACT_ON<. | pvalEXTRACT_END<.
save Data/pvalONENDvIMPACTrobustFIGURE,replace

// FIGURE 1.ii: Onset
tw scatter impactANNUAL_ON pvalANNUAL_ON, m(oh) mc(blue*.5) 						||										///
	lpoly impactANNUAL_ON pvalANNUAL_ON, lc(blue*1.5) lpat(longdash) 				||										///
	scatter impactPERENNIAL_ON pvalPERENNIAL_ON, m(oh) mc(red*.5) 					||										///
	lpoly impactPERENNIAL_ON pvalPERENNIAL_ON, lpat(shortdash) lc(red*1.5) 			||										///
	scatter impactEXTRACT_ON pvalEXTRACT_ON, m(oh) mc(green*.5) 					||										///
	lpoly impactEXTRACT_ON pvalEXTRACT_ON, lpat(solid) lc(green*1.5) xline(0.05, lpattern(dash) lc(gs5))					///
	xti("p-value on sum of shocks", margin(top)) yti("% change in conflict risk", margin(right)) yline(0, lc(gs5))			///
	legend(ring(0) pos(2) label(1 "Annual") label(2 "Annual") label(3 "Perennial") label(4 "Perennial") label(5 "Extractive") label(6 "Extractive")) graphr(color(white)) ylab(,angle(360))
graph save TablesFigures/pvalONvIMPACT-FIGURE1ii, replace
graph export TablesFigures/pvalONvIMPACT-FIGURE1ii.eps,replace

// FIGURE 4.ii: Ending
tw scatter impactANNUAL_END pvalANNUAL_END, m(oh) mc(blue*.5) 						||										///
	lpoly impactANNUAL_END pvalANNUAL_END, lc(blue*1.5) lpat(longdash) 				||										///
	scatter impactPERENNIAL_END pvalPERENNIAL_END, m(oh) mc(red*.5) 				||										///
	lpoly impactPERENNIAL_END pvalPERENNIAL_END, lpat(shortdash) lc(red*1.5) 		||										///
	scatter impactEXTRACT_END pvalEXTRACT_END, m(oh) mc(green*.5) 					||										///
	lpoly impactEXTRACT_END pvalEXTRACT_END, lpat(solid) lc(green*1.5) xline(0.05, lpattern(dash) lc(gs5))					///
	xti("p-value on sum of shocks", margin(top)) yti("% change in peace risk", margin(right)) yline(0, lc(gs5)) 			///
	legend(ring(0) pos(2) label(1 "Annual") label(2 "Annual") label(3 "Perennial") label(4 "Perennial") label(5 "Extractive") label(6 "Extractive")) graphr(color(white)) ylab(,angle(360))
graph save TablesFigures/pvalENDvIMPACT-FIGURE4ii, replace
graph export TablesFigures/pvalENDvIMPACT-FIGURE4ii.eps,replace

restore

use Data/pvalONENDvIMPACTrobustFIGURE,clear
keep pval* convergence*
rename pvalANNUAL_ON pvalONannual
rename pvalANNUAL_END pvalENDannual
rename pvalPERENNIAL_ON pvalONperennial
rename pvalPERENNIAL_END pvalENDperennial
rename pvalEXTRACT_ON pvalONextractive
rename pvalEXTRACT_END pvalENDextractive

rename pvalANNUAL_ON_LOG pvalONannualLOG
rename pvalANNUAL_END_LOG pvalENDannualLOG
rename pvalPERENNIAL_ON_LOG pvalONperennialLOG
rename pvalPERENNIAL_END_LOG pvalENDperennialLOG
rename pvalEXTRACT_ON_LOG pvalONextractiveLOG
rename pvalEXTRACT_END_LOG pvalENDextractiveLOG

preserve
keep *LOG
rensfix LOG
drop in 61/66
save Data/pvalLOGITdisag.dta, replace
restore

drop *LOG
append using Data/pvalLOGITdisag
gen row = _n
merge 1:1 row using Data/specDepvar
drop _m
reshape long pvalON pvalEND, i(row) j(commod) string
save Data/pvalONENDrobustDISAGGfigure,replace

// FIGURE 1.i: Onset
count if convergenceON==1 & lpm==0 & commod=="annual" & pvalON<.
local conv = r(N)
local N = 60
tw hist pvalON if lpm==0 & convergenceON==1, start(0) percent w(.05) gap(20) fc(yellow*.3) || hist pvalON if lpm==1, percent start(0) w(.05) gap(60) bc(gs3) legend(label(1 "Logit") label(2 "LPM") ring(0) pos(2) row(2)) xti("p-value on sum of shocks", margin(top)) yline(5, lc(red*1.5)) graphr(color(white)) ylab(,angle(360)) note("{it:Note}: variance and p-values could not be calculated for `=`N'-`conv'' out of `N' clogit estimates", size(small)) ylabel(0(5)15)
graph save TablesFigures/pvalON-FIGURE1i, replace
graph export TablesFigures/pvalON-FIGURE1i.eps,replace

// FIGURE 4.i: Ending
count if convergenceEND==1 & lpm==0 & commod=="annual" & pvalEND<.
local conv = r(N)
local N = 60
tw hist pvalEND if lpm==0 & convergenceEND==1, percent w(.05) gap(20) fc(yellow*.3) || hist pvalEND if lpm==1, percent w(.05) gap(60) bc(gs3) legend(label(1 "Logit") label(2 "LPM") ring(0) pos(2) row(2)) xti("p-value on sum of shocks", margin(top)) yline(5, lc(red*1.5)) graphr(color(white)) ylab(,angle(360)) note("{it:Note}: variance and p-values could not be calculated for `=`N'-`conv'' out of `N' clogit estimates", size(small))
graph save TablesFigures/pvalEND-FIGURE4i, replace
graph export TablesFigures/pvalEND-FIGURE4i.eps,replace


*---------------------------------------------------------------------------------
// TABLE 6: P-VALUE REGRESSION
*---------------------------------------------------------------------------------

ren row regNum
gen logit = lpm==0

* commodity
gen annual = commod=="annual"
gen perennial = commod=="perennial"
gen extractive = commod=="extractive"

* dependent variable
egen depvarNum = group(depvar) 
gen COW = depvar=="war_cow"
gen FL = depvar=="war_fl"
gen PRIOBB = depvar=="war_prio_bb"
gen PRIOMSS = depvar=="war_prio_mss"
gen SAMB = depvar=="war_samb"

* specification choice
gen NoImport = spec=="importDrop"
gen NoCountryFE = spec=="countryFEdrop"
gen NoYearFE = spec=="yearFEdrop"
gen timeTrend = spec=="timeTrend"
gen fixedWt = spec=="fixedweight"
gen NoX2GDP = spec=="noX2GDP"
gen NoOutlier = spec=="outlierNO"
gen pricemakerIn = spec=="pricemakerIN"
gen pricemaker3PCT = spec=="pricemaker3pct"
gen pricemaker20PCT = spec=="pricemaker20pct"

* regs
global table TablesFigures/pvalREG-TABLE6
cap erase $table.xml
cap erase $table.txt

global outR outreg2 using $table, asterisk(se) excel bdec(4) sdec(4) append

* onset
	reg pvalON logit annual perennial COW-pricemaker20PCT if convergenceON==1 | lpm==1, r
	$outR
	
* ending
	reg pvalEND logit annual perennial COW-pricemaker20PCT if convergenceON==1 | lpm==1, r
	$outR


*---------------------------------------------------------------------------------
// FIGURES 2 - 3, 5 - 6: ROBUSTNESS + HETEROGENEITY DISAGG SHOCKS SPECIFICATION
*---------------------------------------------------------------------------------
	
// CALL UP MAIN DATA & RE-PREP FE
use Data/BAZZI_BLATTMAN_FINAL, clear

global IMPX $importCountry

* GET DATA READY
prepFE
prepImport
prepRain	
prepLag	
global annagg sA sA1 sA2
global peragg sP sP1 sP2
global extagg sE sE1 sE2
global X $annagg $peragg $extagg
global annaggsum sA+sA1+sA2
global peraggsum sP+sP1+sP2
global extaggsum sE+sE1+sE2

cap program drop hetFig
program define hetFig
	foreach y in $onset $end {
		
		dis "==========`y'=========="

		if substr("`y'",1,3)=="end" {
			local outF cap outFdisaglincomEND
		}
		else {
			local outF cap outFdisaglincomON
		}

		* -----LPM----- *

		* BASE
		cap drop sA sA1 sA2 sP sP1 sP2 sE sE1 sE2
		gen sA  = pshock_annual_npi_p
		gen sA1 = l.pshock_annual_npi_p
		gen sA2 = l2.pshock_annual_npi_p
		gen sP  = pshock_perennial_npi_p
		gen sP1 = l.pshock_perennial_npi_p
		gen sP2 = l2.pshock_perennial_npi_p
		gen sE  = pshock_extract_npi_p
		gen sE1 = l.pshock_extract_npi_p
		gen sE2 = l2.pshock_extract_npi_p
		cap qui xtreg `y' $X $year $IMPX $country_year $IF, cl($clu) fe 
		`outF'
		
		* no import shock
		cap drop sA sA1 sA2 sP sP1 sP2 sE sE1 sE2
		gen sA  = pshock_annual_npi_p
		gen sA1 = l.pshock_annual_npi_p
		gen sA2 = l2.pshock_annual_npi_p
		gen sP  = pshock_perennial_npi_p
		gen sP1 = l.pshock_perennial_npi_p
		gen sP2 = l2.pshock_perennial_npi_p
		gen sE  = pshock_extract_npi_p
		gen sE1 = l.pshock_extract_npi_p
		gen sE2 = l2.pshock_extract_npi_p
		cap qui xtreg `y' $X $year $country_year $IF, cl($clu) fe 
		`outF'		
		
		* no x/gdp adjustment
		cap drop sA sA1 sA2 sP sP1 sP2 sE sE1 sE2
		gen sA  = z_rpgr_annual_npi_p
		gen sA1 = l.z_rpgr_annual_npi_p
		gen sA2 = l2.z_rpgr_annual_npi_p
		gen sP  = z_rpgr_perennial_npi_p
		gen sP1 = l.z_rpgr_perennial_npi_p
		gen sP2 = l2.z_rpgr_perennial_npi_p
		gen sE  = z_rpgr_extract_npi_p
		gen sE1 = l.z_rpgr_extract_npi_p
		gen sE2 = l2.z_rpgr_extract_npi_p
		cap qui xtreg `y' $X $year $IMPX $country_year $IF, cl($clu) fe 
		`outF'
		
		* w/ PRICEMAKERS
		cap drop sA sA1 sA2 sP sP1 sP2 sE sE1 sE2
		gen sA  = pshock_annual_npi
		gen sA1 = l.pshock_annual_npi
		gen sA2 = l2.pshock_annual_npi
		gen sP  = pshock_perennial_npi
		gen sP1 = l.pshock_perennial_npi
		gen sP2 = l2.pshock_perennial_npi
		gen sE  = pshock_extract_npi
		gen sE1 = l.pshock_extract_npi
		gen sE2 = l2.pshock_extract_npi
		cap qui xtreg `y' $X $year $IMPX $country_year $IF, cl($clu) fe 
		`outF'
		
		* 3% pricemaker cutoff
		cap drop sA sA1 sA2 sP sP1 sP2 sE sE1 sE2
		gen sA  = pshock_annual_npi_p_03
		gen sA1 = l.pshock_annual_npi_p_03
		gen sA2 = l2.pshock_annual_npi_p_03
		gen sP  = pshock_perennial_npi_p_03
		gen sP1 = l.pshock_perennial_npi_p_03
		gen sP2 = l2.pshock_perennial_npi_p_03
		gen sE  = pshock_extract_npi_p_03
		gen sE1 = l.pshock_extract_npi_p_03
		gen sE2 = l2.pshock_extract_npi_p_03
		cap qui xtreg `y' $X $year $IMPX $country_year $IF, cl($clu) fe 
		`outF'
		
		* 20% pricemaker cutoff
		cap drop sA sA1 sA2 sP sP1 sP2 sE sE1 sE2
		gen sA  = pshock_annual_npi_p_2
		gen sA1 = l.pshock_annual_npi_p_2
		gen sA2 = l2.pshock_annual_npi_p_2
		gen sP  = pshock_perennial_npi_p_2
		gen sP1 = l.pshock_perennial_npi_p_2
		gen sP2 = l2.pshock_perennial_npi_p_2
		gen sE  = pshock_extract_npi_p_2
		gen sE1 = l.pshock_extract_npi_p_2
		gen sE2 = l2.pshock_extract_npi_p_2
		cap qui xtreg `y' $X $year $IMPX $country_year $IF, cl($clu) fe
		`outF'	
		
		* FIXED WEIGHTS
		cap drop sA sA1 sA2 sP sP1 sP2 sE sE1 sE2
		gen sA  = pshock_annual_npf80_p
		gen sA1 = l.pshock_annual_npf80_p
		gen sA2 = l2.pshock_annual_npf80_p
		gen sP  = pshock_perennial_npf80_p
		gen sP1 = l.pshock_perennial_npf80_p
		gen sP2 = l2.pshock_perennial_npf80_p
		gen sE  = pshock_extract_npf80_p
		gen sE1 = l.pshock_extract_npf80_p
		gen sE2 = l2.pshock_extract_npf80_p
		cap qui xtreg `y' $X $year $IMPX $country_year $IF, cl($clu) fe 
		`outF'
		
		* CENSOR OUTLIERS
		cap drop sA sA1 sA2 sP sP1 sP2 sE sE1 sE2
		gen sA  = pshock_annual_npi_p
		gen sA1 = l.pshock_annual_npi_p
		gen sA2 = l2.pshock_annual_npi_p
		gen sP  = pshock_perennial_npi_p
		gen sP1 = l.pshock_perennial_npi_p
		gen sP2 = l2.pshock_perennial_npi_p
		gen sE  = pshock_extract_npi_p
		gen sE1 = l.pshock_extract_npi_p
		gen sE2 = l2.pshock_extract_npi_p
		cap drop keeper
		gen keeper = .
		foreach v in sA sP sE {
			qui su `v', det
			qui replace keeper = `v'>=r(p1) & `v'<=r(p99)
			qui su `v'1, det
			qui replace keeper = `v'1>=r(p1) & `v'1<=r(p99) & keeper==1
			qui su `v'2, det
			qui replace keeper = `v'2>=r(p1) & `v'2<=r(p99) & keeper==1
		}
		cap qui xtreg `y' $X $year $IMPX $country_year $IF & keeper==1, cl($clu) fe 
		`outF'
		
		* DROPPING COUNTRY-SPECIFIC TIME TRENDS
		cap qui xtreg `y' $X $year $IMPX $IF, cl($clu) fe
		`outF'	
		
		* EXCLUDING TIME FEs
		cap qui xtreg `y' $X $IMPX $country_year $IF, cl($clu) fe
		`outF'	
		
		* DROPPING COUNTRY FEs
		cap qui reg `y' $X $year $IMPX $country_year $IF, cl($clu)
		`outF'		

	}
	foreach v in ANNUAL PERENNIAL EXTRACT {
		svmat pval`v'_ON
		svmat pval`v'_END
		svmat impact`v'_ON
		svmat impact`v'_END
	}
	keep if pvalANNUAL_ON<. | pvalANNUAL_END<. | pvalPERENNIAL_ON<. | pvalPERENNIAL_END<. | pvalEXTRACT_ON<. | pvalEXTRACT_END<.
end

global est

* LOW INCOME
qui su gdppc0 if year==1999, det
cap drop missincome
gen missGDP = missing(gdppc0)
gen lowGDP = gdppc0<=r(p50)
cap mat drop pvalANNUAL_END pvalPERENNIAL_END pvalEXTRACT_END pvalANNUAL_ON pvalPERENNIAL_ON pvalEXTRACT_ON
cap mat drop impactANNUAL_END impactPERENNIAL_END impactEXTRACT_END impactANNUAL_ON impactPERENNIAL_ON impactEXTRACT_ON
preserve
keep if lowGDP & !missGDP
hetFig
keep pval* impact*
renpfix pval loINC_pval
renpfix impact loINC_impact
save Data/lowIncomeFigData,replace
restore

* HIGH ETHNIC POLARIZATION
gen missETHPOL = missing(ethpol)
qui su ethpol if year==1999, det
gen highETHPOL = ethpol>=r(p50)
cap mat drop pvalANNUAL_END pvalPERENNIAL_END pvalEXTRACT_END pvalANNUAL_ON pvalPERENNIAL_ON pvalEXTRACT_ON
cap mat drop impactANNUAL_END impactPERENNIAL_END impactEXTRACT_END impactANNUAL_ON impactPERENNIAL_ON impactEXTRACT_ON
preserve
keep if highETHPOL & !missETHPOL
hetFig
keep pval* impact*
renpfix pval hiETHPOL_pval
renpfix impact hiETHPOL_impact
save Data/hiEthPolFigData,replace
restore

* HIGH INEQUALITY
gen missGINI = missing(gini)
su gini if year==1999, det
gen highGINI = gini>=r(p50)
cap mat drop pvalANNUAL_END pvalPERENNIAL_END pvalEXTRACT_END pvalANNUAL_ON pvalPERENNIAL_ON pvalEXTRACT_ON
cap mat drop impactANNUAL_END impactPERENNIAL_END impactEXTRACT_END impactANNUAL_ON impactPERENNIAL_ON impactEXTRACT_ON
preserve
keep if highGINI & !missGINI
hetFig
keep pval* impact*
renpfix pval hiGINI_pval
renpfix impact hiGINI_impact
save Data/hiGiniFigData,replace
restore

* THRICE-LAGGED NON-DEMOCRATIC
gen Lnon_democ = cond(Ldemoc==0,1,0) if Ldemoc<.
bysort cow_id: egen totNonDemoc = total(Lnon_democ), missing
gen everNONDEMOC = totNonDemoc>0 & totNonDemoc<.
gen missNONDEMOC = totNonDemoc>=.
tsset
gen L3non_democ = L2.Lnon_democ
cap mat drop pvalANNUAL_END pvalPERENNIAL_END pvalEXTRACT_END pvalANNUAL_ON pvalPERENNIAL_ON pvalEXTRACT_ON
cap mat drop impactANNUAL_END impactPERENNIAL_END impactEXTRACT_END impactANNUAL_ON impactPERENNIAL_ON impactEXTRACT_ON
preserve
keep if L3non_democ==1
hetFig
keep pval* impact*
renpfix pval L3NonDem_pval
renpfix impact L3NonDem_impact
save Data/L3NonDemocFigData,replace
restore

*--->NOTE: end_war_prio_mss insufficient observations for next two heterogeneous tests
global end end_any_prio end_war_prio_bb end_war_fl end_war_samb end_war_cow

* THRICE-LAGGED ANOCRACY
tsset
gen L3anoc = L2.Lanoc
cap mat drop pvalANNUAL_END pvalPERENNIAL_END pvalEXTRACT_END pvalANNUAL_ON pvalPERENNIAL_ON pvalEXTRACT_ON
cap mat drop impactANNUAL_END impactPERENNIAL_END impactEXTRACT_END impactANNUAL_ON impactPERENNIAL_ON impactEXTRACT_ON
preserve
keep if L3anoc==1
hetFig
keep pval* impact*
renpfix pval L3Anoc_pval
renpfix impact L3Anoc_impact
save Data/L3AnocFigData,replace
restore

* THRICE-LAGGED FACTIONALIZED PARTIAL DEMOCRACY
tsset
gen L3fpdemoc = L2.Lpartial_democ_fac
cap mat drop pvalANNUAL_END pvalPERENNIAL_END pvalEXTRACT_END pvalANNUAL_ON pvalPERENNIAL_ON pvalEXTRACT_ON
cap mat drop impactANNUAL_END impactPERENNIAL_END impactEXTRACT_END impactANNUAL_ON impactPERENNIAL_ON impactEXTRACT_ON
preserve
keep if L3fpdemoc==1
hetFig
keep pval* impact*
renpfix pval L3FPDemoc_pval
renpfix impact L3FPDemoc_impact
save Data/L3FPDemocFigData,replace
restore

global end end_any_prio end_war_prio_bb end_war_prio_mss end_war_fl end_war_samb end_war_cow

* THRICE-LAGGED LOW EXECUTIVE CONSTRAINT
gen low_xconst=0 if xconst < .
replace low_xconst=1 if xconst<=3
tsset
gen L3xconst = L3.low_xconst
cap mat drop pvalANNUAL_END pvalPERENNIAL_END pvalEXTRACT_END pvalANNUAL_ON pvalPERENNIAL_ON pvalEXTRACT_ON
cap mat drop impactANNUAL_END impactPERENNIAL_END impactEXTRACT_END impactANNUAL_ON impactPERENNIAL_ON impactEXTRACT_ON
preserve
keep if L3xconst==1
hetFig
keep pval* impact*
renpfix pval L3xconst_pval
renpfix impact L3xconst_impact
save Data/L3xconstFigData,replace
restore

* SSA
cap mat drop pvalANNUAL_END pvalPERENNIAL_END pvalEXTRACT_END pvalANNUAL_ON pvalPERENNIAL_ON pvalEXTRACT_ON
cap mat drop impactANNUAL_END impactPERENNIAL_END impactEXTRACT_END impactANNUAL_ON impactPERENNIAL_ON impactEXTRACT_ON
preserve
keep if ssa==1
hetFig
keep pval* impact*
renpfix pval SSA_pval
renpfix impact SSA_impact
save Data/SSAFigData,replace
restore

* non-SSA
cap mat drop pvalANNUAL_END pvalPERENNIAL_END pvalEXTRACT_END pvalANNUAL_ON pvalPERENNIAL_ON pvalEXTRACT_ON
cap mat drop impactANNUAL_END impactPERENNIAL_END impactEXTRACT_END impactANNUAL_ON impactPERENNIAL_ON impactEXTRACT_ON
preserve
keep if ssa==0
hetFig
keep pval* impact*
renpfix pval nonSSA_pval
renpfix impact nonSSA_impact
save Data/nonSSAFigData,replace
restore


preserve
use Data/lowIncomeFigData,clear
gen varNum = 1

local i = 1
foreach file in hiEthPol hiGini L3NonDemoc L3Anoc L3FPDemoc L3xconst SSA nonSSA {	
	local i = `i' + 1
	append using Data/`file'FigData
	replace varNum = `i' if varNum>=.
}
bys varNum : gen regNum = _n
collapse *pval* *impact*, by(regNum)


// FIGURE 3.i: annual/onset
local d ANNUAL
local c ON
tw scatter hiETHPOL_impact`d'_`c' hiETHPOL_pval`d'_`c', m(oh) mc(blue*1) 		||												///
	scatter loINC_impact`d'_`c' loINC_pval`d'_`c', m(dh) mc(red*1) 				||												///																
	scatter hiGINI_impact`d'_`c' hiGINI_pval`d'_`c', m(th) mc(green*1) 			||												///
	scatter SSA_impact`d'_`c' SSA_pval`d'_`c', m(x) mc(black*1) 				||												///
	lpoly hiETHPOL_impact`d'_`c' hiETHPOL_pval`d'_`c', lc(blue*1) lpat(solid) 	||												///
	lpoly loINC_impact`d'_`c' loINC_pval`d'_`c', lpat(longdash) lc(red*1) 		||												///
	lpoly hiGINI_impact`d'_`c' hiGINI_pval`d'_`c', lpat(dash) lc(green*1) 		||												///
	lpoly SSA_impact`d'_`c' SSA_pval`d'_`c', lpat(shortdash) lc(black*1)  														///	
	xti("p-value on sum of shocks", margin(top)) 																				///
	yti("% change in conflict risk", margin(right)) yline(0, lc(gs5)) xline(0.05, lpattern(dash) lc(gs5))						///
	legend(label(1 "Hi Eth. Pol.") label(5 "Hi Eth. Pol.") label(2 "Low Income") label(6 "Low Income") 							///
	label(3 "Hi Gini") label(7 "Hi Gini") label(4 "SSA") label(8 "SSA") row(2) size(small)) graphr(color(white)) ylab(,angle(360))
graph save TablesFigures/pval`c'vIMPACT_`d'_HETinc-FIGURE3i,replace
graph export TablesFigures/pval`c'vIMPACT_`d'_HETinc-FIGURE3i.eps,replace
	
	
// FIGURE 3.i: annual/onset
tw scatter L3NonDem_impact`d'_`c' L3NonDem_pval`d'_`c', m(sh) mc(black*1) 			||											///
	scatter L3Anoc_impact`d'_`c' L3Anoc_pval`d'_`c', m(x) mc(yellow*1.5) 			||											///
	scatter L3xconst_impact`d'_`c' L3xconst_pval`d'_`c', m(oh) mc(green*1) 			||											///
	scatter L3FPDemoc_impact`d'_`c' L3FPDemoc_pval`d'_`c', m(dh) mc(red*1) 			||											///
	lpoly L3NonDem_impact`d'_`c' L3NonDem_pval`d'_`c', lpat(solid) lc(black*1) 		||											///
	lpoly L3Anoc_impact`d'_`c' L3Anoc_pval`d'_`c', lpat(longdash) lc(yellow*1.5) 	||											///
	lpoly L3xconst_impact`d'_`c' L3xconst_pval`d'_`c', lpat(dash_dot) lc(green*1.5) ||											///
	lpoly L3FPDemoc_impact`d'_`c' L3FPDemoc_pval`d'_`c', lpat(shortdash) lc(red*1) 												///	
	xti("p-value on sum of shocks", margin(top)) 																				///
	yti("% change in conflict risk", margin(right)) yline(0, lc(gs5)) xline(0.05, lpattern(dash) lc(gs5))						///
	legend(label(1 "Non-Democ.") label(5 "Non-Democ.") label(2 "Anocracy") label(6 "Anocracy") 									///
	label(3 "Low Exec. Const.") label(7 "Low Exec. Const.") label(4 "Fac. Partial Democ.") label(8 "Fac. Partial Democ.") row(2) size(small)) graphr(color(white)) ylab(,angle(360)) 	
graph save TablesFigures/pval`c'vIMPACT_`d'_HETpol-FIGURE2i,replace
graph export TablesFigures/pval`c'vIMPACT_`d'_HETpol-FIGURE2i.eps,replace


// FIGURE 6.i: annual/ending
local d ANNUAL
local c END
tw scatter hiETHPOL_impact`d'_`c' hiETHPOL_pval`d'_`c', m(oh) mc(blue*1) 			||											///
	scatter loINC_impact`d'_`c' loINC_pval`d'_`c', m(dh) mc(red*1)					||											///
	scatter hiGINI_impact`d'_`c' hiGINI_pval`d'_`c', m(th) mc(green*1) 				||											///
	scatter SSA_impact`d'_`c' SSA_pval`d'_`c', m(x) mc(black*1) 					||											///
	lpoly hiETHPOL_impact`d'_`c' hiETHPOL_pval`d'_`c', lc(blue*1) lpat(solid) 		||											///
	lpoly loINC_impact`d'_`c' loINC_pval`d'_`c', lpat(longdash) lc(red*1) 			|| 											///
	lpoly hiGINI_impact`d'_`c' hiGINI_pval`d'_`c', lpat(dash) lc(green*1) 			|| 											///
	lpoly SSA_impact`d'_`c' SSA_pval`d'_`c', lpat(shortdash) lc(black*1)  														///		
	xti("p-value on sum of shocks", margin(top)) 																				///
	yti("% change in peace risk", margin(right)) yline(0, lc(gs5)) xline(0.05, lpattern(dash) lc(gs5))							///
	legend(label(1 "Hi Eth. Pol.") label(5 "Hi Eth. Pol.") label(2 "Low Income") label(6 "Low Income")							///
	label(3 "Hi Gini") label(7 "Hi Gini") label(4 "SSA") label(8 "SSA") row(2) size(small)) graphr(color(white)) ylab(,angle(360))
graph save TablesFigures/pval`c'vIMPACT_`d'_HETinc-FIGURE6i,replace
graph export TablesFigures/pval`c'vIMPACT_`d'_HETinc-FIGURE6i.eps,replace

// FIGURE 6.i: annual/ending - EXCLUDE OUTLIERS
local d ANNUAL
local c END
tw scatter hiETHPOL_impact`d'_`c' hiETHPOL_pval`d'_`c' if hiETHPOL_impact`d'_`c'<5 & hiETHPOL_impact`d'_`c'>-5, m(oh) mc(blue*1) 		||	///
	scatter loINC_impact`d'_`c' loINC_pval`d'_`c' if loINC_impact`d'_`c'<5 & loINC_impact`d'_`c'>-5, m(dh) mc(red*1)					||	///
	scatter hiGINI_impact`d'_`c' hiGINI_pval`d'_`c' if hiGINI_impact`d'_`c'<5 & hiGINI_impact`d'_`c'>-5, m(th) mc(green*1) 			||	///
	scatter SSA_impact`d'_`c' SSA_pval`d'_`c' if SSA_impact`d'_`c'<5 & SSA_impact`d'_`c'>-5, m(x) mc(black*1) 							||	///
	lpoly hiETHPOL_impact`d'_`c' hiETHPOL_pval`d'_`c' if hiETHPOL_impact`d'_`c'<5 & hiETHPOL_impact`d'_`c'>-5, lc(blue*1) lpat(solid) 	||	///
	lpoly loINC_impact`d'_`c' loINC_pval`d'_`c' if loINC_impact`d'_`c'<5 & loINC_impact`d'_`c'>-5, lpat(longdash) lc(red*1) 			||	///
	lpoly hiGINI_impact`d'_`c' hiGINI_pval`d'_`c' if hiGINI_impact`d'_`c'<5 & hiGINI_impact`d'_`c'>-5, lpat(dash) lc(green*1) 			||	///
	lpoly SSA_impact`d'_`c' SSA_pval`d'_`c' if SSA_impact`d'_`c'<5 & SSA_impact`d'_`c'>-5, lpat(shortdash) lc(black*1)  					///
	xti("p-value on sum of shocks", margin(top)) 																				///
	yti("% change in peace risk", margin(right)) yline(0, lc(gs5)) xline(0.05, lpattern(dash) lc(gs5))							///
	legend(label(1 "Hi Eth. Pol.") label(5 "Hi Eth. Pol.") label(2 "Low Income") label(6 "Low Income")							///
	label(3 "Hi Gini") label(7 "Hi Gini") label(4 "SSA") label(8 "SSA") row(2) size(small)) graphr(color(white)) ylab(,angle(360))
graph save TablesFigures/pval`c'vIMPACT_`d'_HETinc-FIGURE6i_outlierNO,replace
graph export TablesFigures/pval`c'vIMPACT_`d'_HETinc-FIGURE6i_outlierNO.eps,replace
	

// FIGURE 5.i: annual/ending
local d ANNUAL
local c END
tw scatter L3NonDem_impact`d'_`c' L3NonDem_pval`d'_`c', m(sh) mc(black*1)			||											///
	scatter L3Anoc_impact`d'_`c' L3Anoc_pval`d'_`c', m(x) mc(yellow*1.5) 			||											///
	scatter L3xconst_impact`d'_`c' L3xconst_pval`d'_`c', m(oh) mc(green*1) 			||											///
	scatter L3FPDemoc_impact`d'_`c' L3FPDemoc_pval`d'_`c', m(dh) mc(red*1) 			||											///
	lpoly L3NonDem_impact`d'_`c' L3NonDem_pval`d'_`c', lpat(solid) lc(black*1) 		||											///
	lpoly L3Anoc_impact`d'_`c' L3Anoc_pval`d'_`c', lpat(longdash) lc(yellow*1.5) 	||											///
	lpoly L3xconst_impact`d'_`c' L3xconst_pval`d'_`c', lpat(dash_dot) lc(green*1.5) || 											///
	lpoly L3FPDemoc_impact`d'_`c' L3FPDemoc_pval`d'_`c', lpat(shortdash) lc(red*1)												///		
	xti("p-value on sum of shocks", margin(top)) 																				///
	yti("% change in peace risk", margin(right)) yline(0, lc(gs5)) xline(0.05, lpattern(dash) lc(gs5))							///
	legend(label(1 "Non-Democ.") label(5 "Non-Democ.") label(2 "Anocracy") label(6 "Anocracy") 									///
	label(3 "Low Exec. Const.") label(7 "Low Exec. Const.") label(4 "Fac. Partial Democ.") label(8 "Fac. Partial Democ.") row(2) size(small)) graphr(color(white)) ylab(,angle(360)) 	
graph save TablesFigures/pval`c'vIMPACT_`d'_HETpol-FIGURE5i,replace
graph export TablesFigures/pval`c'vIMPACT_`d'_HETpol-FIGURE5i.eps,replace

// FIGURE 5.i: annual/ending - EXCLUDE OUTLIERS
local d ANNUAL
local c END
tw scatter L3NonDem_impact`d'_`c' L3NonDem_pval`d'_`c' if L3NonDem_impact`d'_`c'<20 & L3NonDem_impact`d'_`c'>-20, m(sh) mc(black*1)	    		||	///
	scatter L3Anoc_impact`d'_`c' L3Anoc_pval`d'_`c' if L3Anoc_impact`d'_`c'<20 & L3Anoc_impact`d'_`c'>-20, m(x) mc(yellow*1.5) 		    		||	///
	scatter L3xconst_impact`d'_`c' L3xconst_pval`d'_`c' if L3xconst_impact`d'_`c'<20 & L3xconst_impact`d'_`c'>-20, m(oh) mc(green*1)    		||	///
	scatter L3FPDemoc_impact`d'_`c' L3FPDemoc_pval`d'_`c' if L3FPDemoc_impact`d'_`c'<20 & L3FPDemoc_impact`d'_`c'>-20, m(dh) mc(red*1) 			||	///
	lpoly L3NonDem_impact`d'_`c' L3NonDem_pval`d'_`c' if L3NonDem_impact`d'_`c'<20 & L3NonDem_impact`d'_`c'>-20, lpat(solid) lc(black*1) 		||	///
	lpoly L3Anoc_impact`d'_`c' L3Anoc_pval`d'_`c' if L3Anoc_impact`d'_`c'<20 & L3Anoc_impact`d'_`c'>-20, lpat(longdash) lc(yellow*1.5) 			||	///
	lpoly L3xconst_impact`d'_`c' L3xconst_pval`d'_`c' if L3FPDemoc_impact`d'_`c'<20 & L3FPDemoc_impact`d'_`c'>-20, lpat(dash_dot) lc(green*1.5) ||	///
	lpoly L3FPDemoc_impact`d'_`c' L3FPDemoc_pval`d'_`c' if L3FPDemoc_impact`d'_`c'<20 & L3FPDemoc_impact`d'_`c'>-20, lpat(shortdash) lc(red*1)		///
	xti("p-value on sum of shocks", margin(top)) 																				///
	yti("% change in peace risk", margin(right)) yline(0, lc(gs5)) xline(0.05, lpattern(dash) lc(gs5))							///
	legend(label(1 "Non-Democ.") label(5 "Non-Democ.") label(2 "Anocracy") label(6 "Anocracy") 									///
	label(3 "Low Exec. Const.") label(7 "Low Exec. Const.") label(4 "Fac. Partial Democ.") label(8 "Fac. Partial Democ.") row(2) size(small)) graphr(color(white)) ylab(,angle(360)) 	
graph save TablesFigures/pval`c'vIMPACT_`d'_HETpol-FIGURE5i_outlierNO,replace
graph export TablesFigures/pval`c'vIMPACT_`d'_HETpol-FIGURE5i_outlierNO.eps,replace


/*
* perennial/onset
local d PERENNIAL
local c ON
tw scatter hiETHPOL_impact`d'_`c' hiETHPOL_pval`d'_`c', m(oh) mc(blue*1)			||											///
	scatter loINC_impact`d'_`c' loINC_pval`d'_`c', m(dh) mc(red*1) 					||											///
	scatter hiGINI_impact`d'_`c' hiGINI_pval`d'_`c', m(th) mc(green*1) 				||											///
	scatter SSA_impact`d'_`c' SSA_pval`d'_`c', m(x) mc(black*1) 					||											///
	lpoly hiETHPOL_impact`d'_`c' hiETHPOL_pval`d'_`c', lc(blue*1) lpat(solid) 		||											///
	lpoly loINC_impact`d'_`c' loINC_pval`d'_`c', lpat(longdash) lc(red*1) 			||											///
	lpoly hiGINI_impact`d'_`c' hiGINI_pval`d'_`c', lpat(dash) lc(green*1) 			||											///
	lpoly SSA_impact`d'_`c' SSA_pval`d'_`c', lpat(shortdash) lc(black*1)  														///		
	xti("p-value on sum of shocks", margin(top)) 																				///
	yti("% change in conflict risk", margin(right)) yline(0, lc(gs5)) xline(0.05, lpattern(dash) lc(gs5))						///
	legend(label(1 "Hi Eth. Pol.") label(5 "Hi Eth. Pol.") label(2 "Low Income") label(6 "Low Income")							///
	label(3 "Hi Gini") label(7 "Hi Gini") label(4 "SSA") label(8 "SSA") row(2) size(small)) graphr(color(white)) ylab(,angle(360))
graph save TablesFigures/pval`c'vIMPACT_`d'_HETinc,replace
graph export TablesFigures/pval`c'vIMPACT_`d'_HETinc.eps,replace
	
tw scatter L3NonDem_impact`d'_`c' L3NonDem_pval`d'_`c', m(sh) mc(black*1) 			||											///
	scatter L3Anoc_impact`d'_`c' L3Anoc_pval`d'_`c', m(x) mc(yellow*1.5)			||											///
	scatter L3xconst_impact`d'_`c' L3xconst_pval`d'_`c', m(oh) mc(green*1)			||											///
	scatter L3FPDemoc_impact`d'_`c' L3FPDemoc_pval`d'_`c', m(dh) mc(red*1)			||											///
	lpoly L3NonDem_impact`d'_`c' L3NonDem_pval`d'_`c', lpat(solid) lc(black*1) 		||											///
	lpoly L3Anoc_impact`d'_`c' L3Anoc_pval`d'_`c', lpat(longdash) lc(yellow*1.5) 	||											///
	lpoly L3xconst_impact`d'_`c' L3xconst_pval`d'_`c', lpat(dash_dot) lc(green*1.5) ||											///
	lpoly L3FPDemoc_impact`d'_`c' L3FPDemoc_pval`d'_`c', lpat(shortdash) lc(red*1)  											///		
	xti("p-value on sum of shocks", margin(top)) 																				///
	yti("% change in conflict risk", margin(right)) yline(0, lc(gs5)) xline(0.05, lpattern(dash) lc(gs5))						///
	legend(label(1 "Non-Democ.") label(5 "Non-Democ.") label(2 "Anocracy") label(6 "Anocracy") 									///
	label(3 "Low Exec. Const.") label(7 "Low Exec. Const.") label(4 "Fac. Partial Democ.") label(8 "Fac. Partial Democ.") row(2) size(small)) graphr(color(white)) ylab(,angle(360)) 		
graph save TablesFigures/pval`c'vIMPACT_`d'_HETpol,replace
graph export TablesFigures/pval`c'vIMPACT_`d'_HETpol.eps,replace


* perennial/end
local c END
tw scatter hiETHPOL_impact`d'_`c' hiETHPOL_pval`d'_`c', m(oh) mc(blue*1) 			||											///
	scatter loINC_impact`d'_`c' loINC_pval`d'_`c', m(dh) mc(red*1) 					||											///
	scatter hiGINI_impact`d'_`c' hiGINI_pval`d'_`c', m(th) mc(green*1) 				||											///
	scatter SSA_impact`d'_`c' SSA_pval`d'_`c', m(x) mc(black*1) 					||											///
	lpoly hiETHPOL_impact`d'_`c' hiETHPOL_pval`d'_`c', lc(blue*1) lpat(solid)		||											///
	lpoly loINC_impact`d'_`c' loINC_pval`d'_`c', lpat(longdash) lc(red*1) 			||											///
	lpoly hiGINI_impact`d'_`c' hiGINI_pval`d'_`c', lpat(dash) lc(green*1) 			||											///
	lpoly SSA_impact`d'_`c' SSA_pval`d'_`c', lpat(shortdash) lc(black*1)  														///		
	xti("p-value on sum of shocks", margin(top)) 																				///
	yti("% change in peace risk", margin(right)) yline(0, lc(gs5)) xline(0.05, lpattern(dash) lc(gs5))							///
	legend(label(1 "Hi Eth. Pol.") label(5 "Hi Eth. Pol.") label(2 "Low Income") label(6 "Low Income")							///
	label(3 "Hi Gini") label(7 "Hi Gini") label(4 "SSA") label(8 "SSA") row(2) size(small)) graphr(color(white)) ylab(,angle(360))
graph save TablesFigures/pval`c'vIMPACT_`d'_HETinc,replace
graph export TablesFigures/pval`c'vIMPACT_`d'_HETinc.eps,replace
	
tw scatter L3NonDem_impact`d'_`c' L3NonDem_pval`d'_`c', m(sh) mc(black*1) 			||											///
	scatter L3Anoc_impact`d'_`c' L3Anoc_pval`d'_`c', m(x) mc(yellow*1.5)			||											///
	scatter L3xconst_impact`d'_`c' L3xconst_pval`d'_`c', m(oh) mc(green*1) 			||											///
	scatter L3FPDemoc_impact`d'_`c' L3FPDemoc_pval`d'_`c', m(dh) mc(red*1) 			||											///
	lpoly L3NonDem_impact`d'_`c' L3NonDem_pval`d'_`c', lpat(solid) lc(black*1) 		||											///
	lpoly L3Anoc_impact`d'_`c' L3Anoc_pval`d'_`c', lpat(longdash) lc(yellow*1.5) 	||											///
	lpoly L3xconst_impact`d'_`c' L3xconst_pval`d'_`c', lpat(dash_dot) lc(green*1.5) ||											///
	lpoly L3FPDemoc_impact`d'_`c' L3FPDemoc_pval`d'_`c', lpat(shortdash) lc(red*1)  											///		
	xti("p-value on sum of shocks", margin(top)) 																				///
	yti("% change in peace risk", margin(right)) yline(0, lc(gs5)) xline(0.05, lpattern(dash) lc(gs5))							///
	legend(label(1 "Non-Democ.") label(5 "Non-Democ.") label(2 "Anocracy") label(6 "Anocracy") 									///
	label(3 "Low Exec. Const.") label(7 "Low Exec. Const.") label(4 "Fac. Partial Democ.") label(8 "Fac. Partial Democ.") row(2) size(small)) graphr(color(white)) ylab(,angle(360)) 		
graph save TablesFigures/pval`c'vIMPACT_`d'_HETpol,replace
graph export TablesFigures/pval`c'vIMPACT_`d'_HETpol.eps,replace
*/

// FIGURE 3.ii: extract/onset
local d EXTRACT
local c ON
tw scatter hiETHPOL_impact`d'_`c' hiETHPOL_pval`d'_`c', m(oh) mc(blue*1) 			||											///
	scatter loINC_impact`d'_`c' loINC_pval`d'_`c', m(dh) mc(red*1) 					||											///
	scatter hiGINI_impact`d'_`c' hiGINI_pval`d'_`c', m(th) mc(green*1) 				||											///
	scatter SSA_impact`d'_`c' SSA_pval`d'_`c', m(x) mc(black*1) 					||											///
	lpoly hiETHPOL_impact`d'_`c' hiETHPOL_pval`d'_`c', lc(blue*1) lpat(solid) 		||											///
	lpoly loINC_impact`d'_`c' loINC_pval`d'_`c', lpat(longdash) lc(red*1) 			||											///
	lpoly hiGINI_impact`d'_`c' hiGINI_pval`d'_`c', lpat(dash) lc(green*1)  			||											///
	lpoly SSA_impact`d'_`c' SSA_pval`d'_`c', lpat(shortdash) lc(black*1)  														///		
	xti("p-value on sum of shocks", margin(top)) 																				///
	yti("% change in conflict risk", margin(right)) yline(0, lc(gs5)) xline(0.05, lpattern(dash) lc(gs5))						///
	legend(label(1 "Hi Eth. Pol.") label(5 "Hi Eth. Pol.") label(2 "Low Income") label(6 "Low Income") 							///
	label(3 "Hi Gini") label(7 "Hi Gini") label(4 "SSA") label(8 "SSA") row(2) size(small)) graphr(color(white)) ylab(,angle(360))
graph save TablesFigures/pval`c'vIMPACT_`d'_HETinc-FIGURE3ii,replace
graph export TablesFigures/pval`c'vIMPACT_`d'_HETinc-FIGURE3ii.eps,replace
	
	
// FIGURE 2.ii: extract/onset
tw scatter L3NonDem_impact`d'_`c' L3NonDem_pval`d'_`c', m(sh) mc(black*1)  			||											///
	scatter L3Anoc_impact`d'_`c' L3Anoc_pval`d'_`c', m(x) mc(yellow*1.5)  			||											///
	scatter L3xconst_impact`d'_`c' L3xconst_pval`d'_`c', m(oh) mc(green*1)  		||											///
	scatter L3FPDemoc_impact`d'_`c' L3FPDemoc_pval`d'_`c', m(dh) mc(red*1)  		||											///
	lpoly L3NonDem_impact`d'_`c' L3NonDem_pval`d'_`c', lpat(solid) lc(black*1)  	||											///
	lpoly L3Anoc_impact`d'_`c' L3Anoc_pval`d'_`c', lpat(longdash) lc(yellow*1.5)  	||											///
	lpoly L3xconst_impact`d'_`c' L3xconst_pval`d'_`c', lpat(dash_dot) lc(green*1.5) ||											///
	lpoly L3FPDemoc_impact`d'_`c' L3FPDemoc_pval`d'_`c', lpat(shortdash) lc(red*1)  											///		
	xti("p-value on sum of shocks", margin(top)) 																				///
	yti("% change in conflict risk", margin(right)) yline(0, lc(gs5)) xline(0.05, lpattern(dash) lc(gs5))						///
	legend(label(1 "Non-Democ.") label(5 "Non-Democ.") label(2 "Anocracy") label(6 "Anocracy")									///
	label(3 "Low Exec. Const.") label(7 "Low Exec. Const.") label(4 "Fac. Partial Democ.") label(8 "Fac. Partial Democ.") row(2) size(small)) graphr(color(white)) ylab(,angle(360)) 	
graph save TablesFigures/pval`c'vIMPACT_`d'_HETpol-FIGURE2ii,replace
graph export TablesFigures/pval`c'vIMPACT_`d'_HETpol-FIGURE2ii.eps,replace


// FIGURE 6.ii: extract/ending
local c END
tw scatter hiETHPOL_impact`d'_`c' hiETHPOL_pval`d'_`c', m(oh) mc(blue*1)  			||											///
	scatter loINC_impact`d'_`c' loINC_pval`d'_`c', m(dh) mc(red*1)  				||											///
	scatter hiGINI_impact`d'_`c' hiGINI_pval`d'_`c', m(th) mc(green*1)  			||											///
	scatter SSA_impact`d'_`c' SSA_pval`d'_`c', m(x) mc(black*1)  					||											///
	lpoly hiETHPOL_impact`d'_`c' hiETHPOL_pval`d'_`c', lc(blue*1) lpat(solid)  		||											///
	lpoly loINC_impact`d'_`c' loINC_pval`d'_`c', lpat(longdash) lc(red*1)  			||											///
	lpoly hiGINI_impact`d'_`c' hiGINI_pval`d'_`c', lpat(dash) lc(green*1)  			||											///
	lpoly SSA_impact`d'_`c' SSA_pval`d'_`c', lpat(shortdash) lc(black*1)  														///		
	xti("p-value on sum of shocks", margin(top)) 																				///
	yti("% change in peace risk", margin(right)) yline(0, lc(gs5)) xline(0.05, lpattern(dash) lc(gs5))							///
	legend(label(1 "Hi Eth. Pol.") label(5 "Hi Eth. Pol.") label(2 "Low Income") label(6 "Low Income") 							///
	label(3 "Hi Gini") label(7 "Hi Gini") label(4 "SSA") label(8 "SSA") row(2) size(small)) graphr(color(white)) ylab(,angle(360))
graph save TablesFigures/pval`c'vIMPACT_`d'_HETinc-FIGURE6ii,replace
graph export TablesFigures/pval`c'vIMPACT_`d'_HETinc-FIGURE6ii.eps,replace

// FIGURE 6.ii: extract/ending - EXCLUDE OUTLIERS
local d EXTRACT
local c END
tw scatter hiETHPOL_impact`d'_`c' hiETHPOL_pval`d'_`c' if hiETHPOL_impact`d'_`c'<5 & hiETHPOL_impact`d'_`c'>-10, m(oh) mc(blue*1) 		||	///
	scatter loINC_impact`d'_`c' loINC_pval`d'_`c' if loINC_impact`d'_`c'<5 & loINC_impact`d'_`c'>-10, m(dh) mc(red*1)  			  		||	///
	scatter hiGINI_impact`d'_`c' hiGINI_pval`d'_`c' if hiGINI_impact`d'_`c'<5 & hiGINI_impact`d'_`c'>-10, m(th) mc(green*1)  	  		||	///
	scatter SSA_impact`d'_`c' SSA_pval`d'_`c' if SSA_impact`d'_`c'<5 & SSA_impact`d'_`c'>-10, m(x) mc(black*1)  				  		||	///
	lpoly hiETHPOL_impact`d'_`c' hiETHPOL_pval`d'_`c' if hiETHPOL_impact`d'_`c'<5 & hiETHPOL_impact`d'_`c'>-10, lc(blue*1) lpat(solid)  ||	///
	lpoly loINC_impact`d'_`c' loINC_pval`d'_`c' if loINC_impact`d'_`c'<5 & loINC_impact`d'_`c'>-10, lpat(longdash) lc(red*1)  			||	///	
	lpoly hiGINI_impact`d'_`c' hiGINI_pval`d'_`c' if hiGINI_impact`d'_`c'<5 & hiGINI_impact`d'_`c'>-10, lpat(dash) lc(green*1)  		||	///
	lpoly SSA_impact`d'_`c' SSA_pval`d'_`c' if SSA_impact`d'_`c'<5 & SSA_impact`d'_`c'>-10, lpat(shortdash) lc(black*1)  					///
	xti("p-value on sum of shocks", margin(top)) 																				///
	yti("% change in peace risk", margin(right)) yline(0, lc(gs5)) xline(0.05, lpattern(dash) lc(gs5))							///
	legend(label(1 "Hi Eth. Pol.") label(5 "Hi Eth. Pol.") label(2 "Low Income") label(6 "Low Income") 							///
	label(3 "Hi Gini") label(7 "Hi Gini") label(4 "SSA") label(8 "SSA") row(2) size(small)) graphr(color(white)) ylab(,angle(360))
graph save TablesFigures/pval`c'vIMPACT_`d'_HETinc-FIGURE6ii_outlierNO,replace
graph export TablesFigures/pval`c'vIMPACT_`d'_HETinc-FIGURE6ii_outlierNO.eps,replace	

// FIGURE 5.ii: extract/ending 
local d EXTRACT
local c END
tw scatter L3NonDem_impact`d'_`c' L3NonDem_pval`d'_`c', m(sh) mc(black*1) 			||	///
	scatter L3Anoc_impact`d'_`c' L3Anoc_pval`d'_`c', m(x) mc(yellow*1.5) 					||	///
	scatter L3xconst_impact`d'_`c' L3xconst_pval`d'_`c', m(oh) mc(green*1)  			||	///
	scatter L3FPDemoc_impact`d'_`c' L3FPDemoc_pval`d'_`c', m(dh) mc(red*1)  		||	///
	lpoly L3NonDem_impact`d'_`c' L3NonDem_pval`d'_`c', lpat(solid) lc(black*1)  		||	///
	lpoly L3Anoc_impact`d'_`c' L3Anoc_pval`d'_`c', lpat(longdash) lc(yellow*1.5)  		||	///
	lpoly L3xconst_impact`d'_`c' L3xconst_pval`d'_`c', lpat(dash_dot) lc(green*1.5) 	||	///
	lpoly L3FPDemoc_impact`d'_`c' L3FPDemoc_pval`d'_`c', lpat(shortdash) lc(red*1)		///
	xti("p-value on sum of shocks", margin(top)) 																				///
	yti("% change in peace risk", margin(right)) yline(0, lc(gs5)) xline(0.05, lpattern(dash) lc(gs5))							///
	legend(label(1 "Non-Democ.") label(5 "Non-Democ.") label(2 "Anocracy") label(6 "Anocracy") 									///
	label(3 "Low Exec. Const.") label(7 "Low Exec. Const.") label(4 "Fac. Partial Democ.") label(8 "Fac. Partial Democ.") row(2) size(small)) graphr(color(white)) ylab(,angle(360)) 	
graph save TablesFigures/pval`c'vIMPACT_`d'_HETpol-FIGURE5ii,replace
graph export TablesFigures/pval`c'vIMPACT_`d'_HETpol-FIGURE5ii.eps,replace

// FIGURE 5.ii: extract/ending - EXCLUDE OUTLIERS
local d EXTRACT
local c END
tw scatter L3NonDem_impact`d'_`c' L3NonDem_pval`d'_`c' if L3NonDem_impact`d'_`c'<20 & L3NonDem_impact`d'_`c'>-20, m(sh) mc(black*1) 			||	///
	scatter L3Anoc_impact`d'_`c' L3Anoc_pval`d'_`c' if L3Anoc_impact`d'_`c'<20 & L3Anoc_impact`d'_`c'>-20, m(x) mc(yellow*1.5) 					||	///
	scatter L3xconst_impact`d'_`c' L3xconst_pval`d'_`c' if L3xconst_impact`d'_`c'<20 & L3xconst_impact`d'_`c'>-20, m(oh) mc(green*1)  			||	///
	scatter L3FPDemoc_impact`d'_`c' L3FPDemoc_pval`d'_`c' if L3FPDemoc_impact`d'_`c'<20 & L3FPDemoc_impact`d'_`c'>-20, m(dh) mc(red*1)  		||	///
	lpoly L3NonDem_impact`d'_`c' L3NonDem_pval`d'_`c' if L3NonDem_impact`d'_`c'<20 & L3NonDem_impact`d'_`c'>-20, lpat(solid) lc(black*1)  		||	///
	lpoly L3Anoc_impact`d'_`c' L3Anoc_pval`d'_`c' if L3Anoc_impact`d'_`c'<20 & L3Anoc_impact`d'_`c'>-20, lpat(longdash) lc(yellow*1.5)  		||	///
	lpoly L3xconst_impact`d'_`c' L3xconst_pval`d'_`c' if L3xconst_impact`d'_`c'<20 & L3xconst_impact`d'_`c'>-20, lpat(dash_dot) lc(green*1.5) 	||	///
	lpoly L3FPDemoc_impact`d'_`c' L3FPDemoc_pval`d'_`c' if L3FPDemoc_impact`d'_`c'<20 & L3FPDemoc_impact`d'_`c'>-20, lpat(shortdash) lc(red*1)		///
	xti("p-value on sum of shocks", margin(top)) 																				///
	yti("% change in peace risk", margin(right)) yline(0, lc(gs5)) xline(0.05, lpattern(dash) lc(gs5))							///
	legend(label(1 "Non-Democ.") label(5 "Non-Democ.") label(2 "Anocracy") label(6 "Anocracy") 									///
	label(3 "Low Exec. Const.") label(7 "Low Exec. Const.") label(4 "Fac. Partial Democ.") label(8 "Fac. Partial Democ.") row(2) size(small)) graphr(color(white)) ylab(,angle(360)) 	
graph save TablesFigures/pval`c'vIMPACT_`d'_HETpol-FIGURE5ii_outlierNO,replace
graph export TablesFigures/pval`c'vIMPACT_`d'_HETpol-FIGURE5ii_outlierNO.eps,replace

restore


*---------------------------------------------------------------------------------
// BATTLE DEATHS ANALYSIS: VARIABLE CODING
*---------------------------------------------------------------------------------

* Impute missing battle deaths

	* Indicator for imputation
		gen impute = cond(any_prio==1 & totbdeadbes==.,1,0)

	* Impute missing battle deaths as the average of high and low 
		gen totbdeadbes_impute=totbdeadbes
		replace totbdeadbes_impute=(totbdeadhig+totbdeadlow)/2 if any_prio==1 & totbdeadbes_impute==.

* Create new interval measure replacing hi/low with best where available
	gen totbdead_int_hi = totbdeadbes
	replace totbdead_int_hi = totbdeadhig if totbdead_int_hi==. & any_prio==1

	gen totbdead_int_lo = totbdeadbes
	replace totbdead_int_lo = totbdeadlow if totbdead_int_lo==. & any_prio==1

* Lagged battle deaths
	sort cow_id year
	gen Ltotbdeadbes=l.totbdeadbes
	replace Ltotbdeadbes=0 if Ltotbdeadbes==. & any_prio==1
	
	sort cow_id year
	gen Ltotbdeadbes_impute=l.totbdeadbes_impute
	replace Ltotbdeadbes_impute=0 if Ltotbdeadbes_impute==. & any_prio==1

	sort cow_id year
	gen Ltotbdead_int_hi=l.totbdead_int_hi
	replace Ltotbdead_int_hi=0 if Ltotbdead_int_hi==. & any_prio==1
	
	sort cow_id year
	gen Ltotbdead_int_lo=l.totbdead_int_lo
	replace Ltotbdead_int_lo=0 if Ltotbdead_int_lo==. & any_prio==1
	
* Log terms
	gen ltotbdeadbes = ln(totbdeadbes)
	gen lLtotbdeadbes = ln(Ltotbdeadbes)
	gen ltotbdead_int_hi = ln(totbdead_int_hi)
	gen ltotbdead_int_lo = ln(totbdead_int_lo)
	gen ltotbdeadbes_impute = ln(totbdeadbes_impute)
	gen lLtotbdeadbes_impute = ln(Ltotbdeadbes_impute)
	
* Indicators for first year of conflict and for conflict duration
	gen first_year=cond(totbdeadbes_impute!=. & end_any_prio==.,1,0) if any_prio==1

	gen duration=first_year
	sort cow_id year
	replace duration=l.duration+1 if first_year!=1 & any_prio==1

	
*---------------------------------------------------------------------------------
// TABLE 7: BATTLE DEATHS
*---------------------------------------------------------------------------------
	global Xreg mena ssa asia americas

	global agg pshock_npi_p pshock_npi_p1 pshock_npi_p2
	global X $agg
	global aggsum pshock_npi_p+pshock_npi_p1+pshock_npi_p2
	global Xsum $aggsum

	global table TablesFigures/DEATHSAGG-TABLE7
	cap erase $table.xml
	cap erase $table.txt

	** Linear **

	* Starting linear interval regression (no LDV)
	intreg totbdead_int_lo totbdead_int_hi $X duration first_year $Xreg $year $IF, cl($clu)
		outFlincom

	* Add lagged dependent variable (imputed, rather than hi/lo)
	intreg totbdead_int_lo totbdead_int_hi $X duration first_year Ltotbdeadbes_impute $Xreg $year $IF, cl($clu)
		outFlincom		

	* Add lagged dependent variable (not imputed)
	intreg totbdead_int_lo totbdead_int_hi $X duration first_year Ltotbdeadbes $Xreg $year if impute==0, cl($clu)
		outFlincom		
				
		
	** Logs **
		
	* Starting log interval regression (no LDV)
	intreg ltotbdead_int_lo ltotbdead_int_hi $X duration first_year $Xreg $year $IF, cl($clu)
		outFlincom

	* Add lagged dependent variable (linear and imputed, rather than hi/lo)
	intreg ltotbdead_int_lo ltotbdead_int_hi $X duration first_year Ltotbdeadbes_impute $Xreg $year $IF, cl($clu)
		outFlincom			

	* Add lagged dependent variable (not imputed)
	intreg ltotbdead_int_lo ltotbdead_int_hi $X duration first_year Ltotbdeadbes $Xreg $year if impute==0, cl($clu)
		outFlincom			
		
		
	// ADD IMPORT PRICE SHOCKS
	global IMPX $importRegion 
	global Xreg mena ssa asia americas

	global table TablesFigures/DEATHSAGG-TABLE7-IMPORTSHOCK
	cap erase $table.xml
	cap erase $table.txt

	** Linear **

	* Starting linear interval regression (no LDV)
	intreg totbdead_int_lo totbdead_int_hi $X duration first_year $importRegion $Xreg $year $IF, cl($clu)
		outFlincomIMPORT

	* Add lagged dependent variable (imputed, rather than hi/lo)
	intreg totbdead_int_lo totbdead_int_hi $X duration first_year Ltotbdeadbes_impute $importRegion $Xreg $year $IF, cl($clu)
		outFlincomIMPORT		

	* Add lagged dependent variable (not imputed)
	intreg totbdead_int_lo totbdead_int_hi $X duration first_year Ltotbdeadbes $importRegion $Xreg $year if impute==0, cl($clu)
		outFlincomIMPORT		
				
		
	** Logs **
		
	* Starting log interval regression (no LDV)
	intreg ltotbdead_int_lo ltotbdead_int_hi $X duration first_year $importRegion $Xreg $year $IF, cl($clu)
		outFlincomIMPORT

	* Add lagged dependent variable (linear and imputed, rather than hi/lo)
	intreg ltotbdead_int_lo ltotbdead_int_hi $X duration first_year Ltotbdeadbes_impute $importRegion $Xreg $year $IF, cl($clu)
		outFlincomIMPORT			

	* Add lagged dependent variable (not imputed)
	intreg ltotbdead_int_lo ltotbdead_int_hi $X duration first_year Ltotbdeadbes $importRegion $Xreg $year if impute==0, cl($clu)
		outFlincomIMPORT			


*---------------------------------------------------------------------------------
// TABLES 8: DISAGGREGATED BATTLE DEATHS
*---------------------------------------------------------------------------------

global annagg pshock_annual_npi_p pshock_annual_npi_p1 pshock_annual_npi_p2
global peragg pshock_perennial_npi_p pshock_perennial_npi_p1 pshock_perennial_npi_p2
global extagg pshock_extract_npi_p pshock_extract_npi_p1 pshock_extract_npi_p2
global X $annagg $peragg $extagg
global annaggsum pshock_annual_npi_p+pshock_annual_npi_p1+pshock_annual_npi_p2
global peraggsum pshock_perennial_npi_p+pshock_perennial_npi_p1+pshock_perennial_npi_p2
global extaggsum pshock_extract_npi_p+pshock_extract_npi_p1+pshock_extract_npi_p2

cap mat drop pvalANNUAL pvalPERENNIAL pvalEXTRACT

global table TablesFigures/DEATHSDISAGG-TABLEA13
cap erase $table.xml
cap erase $table.txt


** Linear **

	* Starting linear interval regression (no LDV)
	intreg totbdead_int_lo totbdead_int_hi $X duration first_year $Xreg $year $IF, cl($clu)
		outFdisaglincom

	* Add lagged dependent variable (imputed, rather than hi/lo)
	intreg totbdead_int_lo totbdead_int_hi $X duration first_year Ltotbdeadbes_impute $Xreg $year $IF, cl($clu)
		outFdisaglincom	

	* Add lagged dependent variable (not imputed)
	intreg totbdead_int_lo totbdead_int_hi $X duration first_year Ltotbdeadbes $Xreg $year if impute==0, cl($clu)
		outFdisaglincom			
		
** Logs **
		
	* Starting linear interval regression (no LDV, no FE)
	intreg ltotbdead_int_lo ltotbdead_int_hi $X duration first_year $Xreg $year $IF, cl($clu)
		outFdisaglincom

	* Add lagged dependent variable (linear and imputed, rather than hi/lo)
	intreg ltotbdead_int_lo ltotbdead_int_hi $X duration first_year Ltotbdeadbes_impute $Xreg $year $IF, cl($clu)
		outFdisaglincom	

	* Add lagged dependent variable (not imputed)
	intreg ltotbdead_int_lo ltotbdead_int_hi $X duration first_year Ltotbdeadbes $Xreg $year if impute==0, cl($clu)
		outFdisaglincom	

preserve
svmat pvalANNUAL 
svmat pvalPERENNIAL 
svmat pvalEXTRACT
keep if pvalANNUAL<.
keep pval*
save TablesFigures/pval-DEATHSDISAGG,replace
restore
		
	
*IMPORT SHOCKS

cap mat drop pvalANNUAL pvalPERENNIAL pvalEXTRACT

global IMPX $importRegion

global table TablesFigures/DEATHSDISAGG-TABLE8-IMPORTSHOCK
cap erase $table.xml
cap erase $table.txt


** Linear **

	* Starting linear interval regression (no LDV)
	intreg totbdead_int_lo totbdead_int_hi $X duration first_year $importRegion $Xreg $year $IF, cl($clu)
		outFdisaglincomIMPORT

	* Add lagged dependent variable (imputed, rather than hi/lo)
	intreg totbdead_int_lo totbdead_int_hi $X duration first_year Ltotbdeadbes_impute $importRegion $Xreg $year $IF, cl($clu)
		outFdisaglincomIMPORT	

	* Add lagged dependent variable (not imputed)
	intreg totbdead_int_lo totbdead_int_hi $X duration first_year Ltotbdeadbes $importRegion $Xreg $year if impute==0, cl($clu)
		outFdisaglincomIMPORT			
		
** Logs **
		
	* Starting linear interval regression (no LDV, no FE)
	intreg ltotbdead_int_lo ltotbdead_int_hi $X duration first_year $importRegion $Xreg $year $IF, cl($clu)
		outFdisaglincomIMPORT

	* Add lagged dependent variable (linear and imputed, rather than hi/lo)
	intreg ltotbdead_int_lo ltotbdead_int_hi $X duration first_year Ltotbdeadbes_impute $importRegion $Xreg $year $IF, cl($clu)
		outFdisaglincomIMPORT	

	* Add lagged dependent variable (not imputed)
	intreg ltotbdead_int_lo ltotbdead_int_hi $X duration first_year Ltotbdeadbes $importRegion $Xreg $year if impute==0, cl($clu)
		outFdisaglincomIMPORT	

preserve
svmat pvalANNUAL 
svmat pvalPERENNIAL 
svmat pvalEXTRACT
keep if pvalANNUAL<.
keep pval*
save TablesFigures/pval-DEATHSDISAGGIMPORT,replace
restore


***************************************************************************************************************************************
* APPENDIX FIGURES & TABLES
***************************************************************************************************************************************

*---------------------------------------------------------------------------------
// APPENDIX TABLES 1/2: EFFECT OF RAIN AND COMMOD SHOCKS ON GDP GROWTH
*---------------------------------------------------------------------------------

	/* AGGREGATE SHOCKS */

	global table TablesFigures/GDPGROWTHAGG-TABLEA1
	cap erase $table.xml
	cap erase $table.txt
	
	replace gdplng_wdi = d.lngdppc_wdi

	global X ps ps1 ps2
	global Xsum ps+ps1+ps2
		
	cap drop ps ps1 ps2
	gen ps = pshock_npi_p
	gen ps1 = pshock_npi_p1
	gen ps2 = pshock_npi_p2
	
	* EXCLUDING TIME FEs
	qui xtreg gdplng_wdi $X $country_year if post1960==1, cl($clu) fe
	outFlincom1st

	* DROPPING COUNTRY-SPECIFIC TIME TRENDS
	qui xtreg gdplng_wdi $X $year if post1960==1, cl($clu) fe
	outFlincom1st
	
	* FULL SPEC
	qui xtreg gdplng_wdi $X $year $country_year if post1960==1, cl($clu) fe
	outFlincom1st
	
	* DROPPING COUNTRY FEs
	qui reg gdplng_wdi $X $year $country_year if post1960==1, cl($clu)
	outFlincom1st

	*--ADDING RAINFALL SHOCKS--*
	global X ps ps1 ps2 lnRain lnRain_L1 lnRain_L2
	global Xsum ps + ps1 + ps2
	global XsumR lnRain+lnRain_L1+lnRain_L2

	* EXCLUDING TIME FEs
	qui xtreg gdplng_wdi $X $country_year if post1960==1, cl($clu) fe
	outFlincom1strain
			
	* DROPPING COUNTRY-SPECIFIC TIME TRENDS
	qui xtreg gdplng_wdi $X $year if post1960==1, cl($clu) fe
	outFlincom1strain

	* FULL SPEC
	qui xtreg gdplng_wdi $X $year $country_year if post1960==1, cl($clu) fe
	outFlincom1strain
			
	* DROPPING COUNTRY FEs
	qui reg gdplng_wdi $X $year $country_year if post1960==1, cl($clu)
	outFlincom1strain


	/* DISAGGREGATED SHOCKS */

	global table TablesFigures/GDPGROWTHDISAGG-TABLEA2
	cap erase $table.xml
	cap erase $table.txt	
	
	global annagg sA sA1 sA2
	global peragg sP sP1 sP2
	global extagg sE sE1 sE2
	global X $annagg $peragg $extagg
	global annaggsum sA+sA1+sA2
	global peraggsum sP+sP1+sP2
	global extaggsum sE+sE1+sE2
	
	cap mat drop pvalANNUAL pvalPERENNIAL pvalEXTRACT

	cap drop sA sA1 sA2 sP sP1 sP2 sE sE1 sE2
	gen sA  = pshock_annual_npi_p
	gen sA1 = pshock_annual_npi_p1
	gen sA2 = pshock_annual_npi_p2
	gen sP  = pshock_perennial_npi_p
	gen sP1 = pshock_perennial_npi_p1
	gen sP2 = pshock_perennial_npi_p2
	gen sE  = pshock_extract_npi_p
	gen sE1 = pshock_extract_npi_p1
	gen sE2 = pshock_extract_npi_p2	

	* EXCLUDING TIME FEs
	qui xtreg gdplng_wdi $X $country_year if post1960==1, cl($clu) fe
	outFdisaglincom

		
	* DROPPING COUNTRY-SPECIFIC TIME TRENDS
	qui xtreg gdplng_wdi $X $year if post1960==1, cl($clu) fe
	outFdisaglincom
	
	* FULL SPEC
	qui xtreg gdplng_wdi $X $year $country_year if post1960==1, cl($clu) fe
	outFdisaglincom
	
	* DROPPING COUNTRY FEs
	qui reg gdplng_wdi $X $year $country_year if post1960==1, cl($clu)
	outFdisaglincom

	preserve
	svmat pvalANNUAL 
	svmat pvalPERENNIAL 
	svmat pvalEXTRACT
	keep if pvalANNUAL<.
	keep pval*
	save TablesFigures/pval-GDPGROWTHDISAGG,replace
	restore

	
*---------------------------------------------------------------------------------
// APPENDIX TABLES 3/4: EFFECT OF RAIN AND COMMOD SHOCKS ON OTHER MACRO OUTCOMES
*---------------------------------------------------------------------------------
	
	tsset
	
	foreach v of varlist genGovConsExpUScons HHConsExpUScons {
		gen lnG_`v' = ln(`v'/l.`v')
	}
	
	* AGGREGATED SHOCKS
	global agg pshock_npi_p pshock_npi_p1 pshock_npi_p2
	global X $agg
	global aggsum pshock_npi_p+pshock_npi_p1+pshock_npi_p2
	global Xsum $aggsum
	
	cap mat drop pvalAGGSUM
	global table TablesFigures/BASEAGGMACRO-TABLEA3
	cap erase $table.xml
	cap erase $table.txt
	
	* SET CONSTANT SAMPLE
	qui xtreg lnG_genGovConsExpUScons $X $year $country_year $IF, cl($clu) fe
	qui xtreg lnG_HHConsExpUScons $X $year $country_year $IF & e(sample), cl($clu) fe
	gen sampKeep = e(sample)
		
	foreach y of varlist lnG_HHConsExpUScons lnG_genGovConsExpUScons gdplng_wdi {
		
		qui xtreg `y' $X $year $country_year $IF & sampKeep==1, cl($clu) fe
		outFlincom1st
		
	}
	
	global annagg sA sA1 sA2
	global peragg sP sP1 sP2
	global extagg sE sE1 sE2
	global X $annagg $peragg $extagg
	global annaggsum sA+sA1+sA2
	global peraggsum sP+sP1+sP2
	global extaggsum sE+sE1+sE2
	
	cap drop sA sA1 sA2 sP sP1 sP2 sE sE1 sE2
	gen sA  = pshock_annual_npi_p
	gen sA1 = pshock_annual_npi_p1
	gen sA2 = pshock_annual_npi_p2
	gen sP  = pshock_perennial_npi_p
	gen sP1 = pshock_perennial_npi_p1
	gen sP2 = pshock_perennial_npi_p2
	gen sE  = pshock_extract_npi_p
	gen sE1 = pshock_extract_npi_p1
	gen sE2 = pshock_extract_npi_p2	
	
	cap mat drop pvalANNUAL pvalPERENNIAL pvalEXTRACT
	global table TablesFigures/BASEDISAGGMACRO-TABLEA4
	cap erase $table.xml
	cap erase $table.txt
		
	foreach y of varlist lnG_genGovConsExpUScons lnG_HHConsExpUScons gdplng_wdi {
		
		qui xtreg `y' $X $year $country_year $IF & sampKeep==1, cl($clu) fe
		outFdisaglincom
		
	}

	preserve
	svmat pvalANNUAL 
	svmat pvalPERENNIAL 
	svmat pvalEXTRACT
	keep if pvalANNUAL<.
	keep pval*
	save TablesFigures/pval-BASEDISAGGMACRO,replace
	restore

	
*---------------------------------------------------------------------------------
// APPENDIX TABLES 5/6: SUMMARY STATS ON COMMODITIES
*---------------------------------------------------------------------------------	

	*NOTE: MUST DOWNLOAD CommodityData FOLDER AND PLACE INSIDE DATA FOLDER FOR THE FOLLOWING CODE TO WORK
	
	// TOP 3 EXPORTS BY COUNTRY
	
	preserve
    
    use Data/commod_data_BazziBlattman, clear

    keep if year==1980
    keep country_name w_* 
    drop if country_name==""
    reshape long w_,i(country_name) j(comid) string
    gsort country_name -w_
    by country_name: keep if _n<=3
    by country_name: gen rank=_n
    replace comid = proper(comid)

    reshape wide comid w_,i(country_name) j(rank)
    
    tostring w_1 w_2 w_3, replace force

    forvalues j=1/3 {
        replace comid`j' = comid`j' + " (" + substr(w_`j',1,5) + ")"
        replace comid`j' = subinstr(comid`j',"(0)","(<0.0001)",.)
    }

    keep country_name comid1 comid2 comid3
    
    export excel using TablesFigures/top3commodity, replace

    restore

	// WHICH COUNTRIES ARE PRICEMAKERS IN WHICH YEARS/PRODUCTS

	preserve
	
	use Data/commod_data_BazziBlattman, clear

    foreach v in alum banana barley beef dairy coal cocoa coc_oil coffee copper cotton fish fishml g_oil gnut hide iron jute lamb lead linoil mz gas nickel olvoil orange palm_oil ///
                        pepper oil phos cashew fruit poultry pulp rice rubber shrimp silver ssal soyoil soy sugar tea lumber tin tobacco uranium wheat wool zinc meat swine snoil soymeal cattle ///
                        sheep lv_poul lv_swine lvestock copra sorghum asbest diamond gold {
        
        gen `v'_Pmaker_any57to07 = ishr_world_`v'>=0.1 & ishr_world_`v'<.
        gen `v'_Pmaker_any80 = ishr_world_`v'>=0.1 & ishr_world_`v'<. if year==1980
        
    }
    
    collapse *Pmaker*, by(country_name)
    
    qui count
    local N = r(N)
    
    foreach v in alum banana barley beef dairy coal cocoa coc_oil coffee copper cotton fish fishml g_oil gnut hide iron jute lamb lead linoil mz gas nickel olvoil orange palm_oil ///
                        pepper oil phos cashew fruit poultry pulp rice rubber shrimp silver ssal soyoil soy sugar tea lumber tin tobacco uranium wheat wool zinc meat swine snoil soymeal cattle ///
                        sheep lv_poul lv_swine lvestock copra sorghum asbest diamond gold {
                    
        gen `v'_57to07 = ""
        gen `v'_80 = ""
        
        local i = 1
        while `i'<=`N' {
            replace `v'_57to07 = `v'_57to07 + ", `=country_name[`i']'" if `v'_Pmaker_any57to07[`i']>0 & `v'_Pmaker_any57to07[`i']<.
            replace `v'_80 = `v'_80 + ", `=country_name[`i']'" if `v'_Pmaker_any80[`i']>0 & `v'_Pmaker_any80[`i']<.
            local i = `i' + 1
        }
        
        replace `v'_57to07 = substr(`v'_57to07,3,.) if substr(`v'_57to07,1,2)==", "
        replace `v'_80 = substr(`v'_80,3,.) if substr(`v'_80,1,2)==", "
    
    }    
    
    drop *Pmaker*
    
    #delimit;
    reshape long alum banana barley beef dairy coal cocoa coc_oil coffee copper cotton fish fishml g_oil gnut hide iron jute lamb lead linoil mz gas nickel olvoil orange palm_oil ///
                        pepper oil phos cashew fruit poultry pulp rice rubber shrimp silver ssal soyoil soy sugar tea lumber tin tobacco uranium wheat wool zinc meat swine snoil soymeal cattle ///
                        sheep lv_poul lv_swine lvestock copra sorghum asbest diamond gold, i(country) j(year) string;
    #delimit cr 
    
    drop country
    keep alum - gold
    keep in 1/2
    gen years = "1957-2007" in 1
    replace year = "1980" in 2
    
    export excel using TablesFigures/commodityPmaker, replace
	// * NOTE: Variable (commodity) names may be lost in the export to Excel. Manually copying and pasting the data into Excel will be more helpful.
	
	restore
	
*---------------------------------------------------------------------------------
// APPENDIX TABLES 7/8: CONSTRUCTED FROM TABLE 3 IN MAIN PAPER BLOCK ABOVE
*---------------------------------------------------------------------------------	

		
*---------------------------------------------------------------------------------
// APPENDIX TABLES 9/10: CONSTRUCTED FROM TABLE 5 IN MAIN PAPER BLOCK ABOVE
*---------------------------------------------------------------------------------	

	
*---------------------------------------------------------------------------------
// APPENDIX TABLE 11: BRUCKNER-CICCONE REPLICATION
*---------------------------------------------------------------------------------

gen bcsamp = (war_prio_on!=. & index_g!=. & index_g_l!=. & index_g_l2!=.)
gen bcid = ssa & !inlist(wdi_nm,"Cape Verde","Eritrea","Lesotho","Mauritius","Sao Tome and Principe","Djibouti")

*STANDARDIZING AND LAGGING
	foreach p of varlist rpgr_npi_p_03 rpgr_npi_p1_03 rpgr_npi_p2_03 {
		egen `p'_std = std(`p')
	} 
	replace rpgr_npi_p1_03 = l.rpgr_npi_p_03
	replace rpgr_npi_p2_03 = l2.rpgr_npi_p_03


	foreach p of varlist rpgr_npi_p_03 rpgr_npi_p1_03 rpgr_npi_p2_03 {
		egen `p'_SSA_std = std(`p') if ssa==1
	} 
	replace rpgr_npi_p1_03_SSA_std = l.rpgr_npi_p_03_SSA_std
	replace rpgr_npi_p2_03_SSA_std = l2.rpgr_npi_p_03_SSA_std


	foreach p of varlist rpgr_npi_p_03 rpgr_npi_p1_03 rpgr_npi_p2_03 {
		egen `p'_BC1_std = std(`p') if ssa==1 & year>=1983
	} 
	replace rpgr_npi_p1_03_BC1_std = l.rpgr_npi_p_03_BC1_std
	replace rpgr_npi_p2_03_BC1_std = l2.rpgr_npi_p_03_BC1_std


	foreach p of varlist rpgr_npi_p_03 rpgr_npi_p1_03 rpgr_npi_p2_03 {
		egen `p'_BC2_std = std(`p') if bcsamp==1
	} 
	replace rpgr_npi_p1_03_BC2_std = l.rpgr_npi_p_03_BC2_std
	replace rpgr_npi_p2_03_BC2_std = l2.rpgr_npi_p_03_BC2_std
		
	foreach p of varlist index_g index_g_l index_g_l2 {
		egen `p'_std = std(`p')
	}

global table TablesFigures/BCREP-TABLEA11
cap erase $table.xml
cap erase $table.txt

** BB base case

	global agg pshock_npi_p pshock_npi_p1 pshock_npi_p2
	global X $agg
	global aggsum pshock_npi_p+pshock_npi_p1+pshock_npi_p2
	global Xsum $aggsum

	xtreg onset_war_prio_mss $X $year It*, cl($clu) fe 
	outFlincom
	
** Move to BC shock definition: 3% PM cutoff and no X/GDP adjustment

	global agg rpgr_npi_p_03 rpgr_npi_p1_03 rpgr_npi_p2_03  
	global X $agg
	global aggsum rpgr_npi_p_03+rpgr_npi_p1_03+rpgr_npi_p2_03 
	global Xsum $aggsum

	xtreg onset_war_prio_mss $X $year It*, cl($clu) fe 
	outFlincom
	
** SSA only

	global agg rpgr_npi_p_03_SSA_std rpgr_npi_p1_03_SSA_std rpgr_npi_p2_03_SSA_std  
	global X $agg
	global aggsum rpgr_npi_p_03_SSA_std+rpgr_npi_p1_03_SSA_std+rpgr_npi_p2_03_SSA_std 
	global Xsum $aggsum

	xtreg onset_war_prio_mss $X $year It* if ssa==1, cl($clu) fe 
	outFlincom
	
** SSA and >1983

	global agg rpgr_npi_p_03_BC1_std rpgr_npi_p1_03_BC1_std rpgr_npi_p2_03_BC1_std  
	global X $agg
	global aggsum rpgr_npi_p_03_BC1_std+rpgr_npi_p1_03_BC1_std+rpgr_npi_p2_03_BC1_std 
	global Xsum $aggsum

	xtreg onset_war_prio_mss $X $year It* if ssa==1 & year>=1983, cl($clu) fe 
	outFlincom

** BC sample (missing country-years because of missing shock data)

	global agg rpgr_npi_p_03_BC2_std rpgr_npi_p1_03_BC2_std rpgr_npi_p2_03_BC2_std  
	global X $agg
	global aggsum rpgr_npi_p_03_BC2_std+rpgr_npi_p1_03_BC2_std+rpgr_npi_p2_03_BC2_std 
	global Xsum $aggsum
	 
	xtreg onset_war_prio_mss $X $year It*, cluster($clu) fe
	outFlincom

** BC shock data (standardized)

	global agg index_g_std index_g_l_std index_g_l2_std 
	global X $agg
	global aggsum index_g_std+index_g_l_std+index_g_l2_std 
	global Xsum $aggsum

	xtreg onset_war_prio_mss $X $year It*, cluster($clu) fe
	outFlincom

** Use BC coding of dependent variable

	xtreg war_prio_on $X $year It*, cluster($clu) fe
	outFlincom

** Use ivreg2 

	ivreg2 war_prio_on $X Iccid* $year It*, cluster($clu) partial(Iccid* $year It*)
	outFlincom

** Alternative onset measures

	ivreg2 onset_war_prio_mss $X Iccid* $year It*, cluster($clu) partial(Iccid* $year It*)
	outFlincom

	ivreg2 onset_war_prio_bb $X Iccid* $year It*, cluster($clu) partial(Iccid* $year It*)
	outFlincom

	ivreg2 onset_any_prio $X Iccid* $year It*, cluster($clu) partial(Iccid* $year It*)
	outFlincom

	ivreg2 onset_war_fl $X Iccid* $year It*, cluster($clu) partial(Iccid* $year It*)
	outFlincom

	ivreg2 onset_war_samb $X Iccid* $year It*, cluster($clu) partial(Iccid* $year It*)
	outFlincom

	ivreg2 onset_war_cow $X Iccid* $year It*, cluster($clu) partial(Iccid* $year It*)
	outFlincom	
	
	

*---------------------------------------------------------------------------------
// APPENDIX TABLE 12: DISAGGREGATED SHOCKS ROBUSTNESS
*---------------------------------------------------------------------------------

global annagg sA sA1 sA2
global peragg sP sP1 sP2
global extagg sE sE1 sE2
global X $annagg $peragg $extagg
global annaggsum sA+sA1+sA2
global peraggsum sP+sP1+sP2
global extaggsum sE+sE1+sE2
global IMPX $importCountry
cap mat drop pvalANNUAL pvalPERENNIAL pvalEXTRACT
global table TablesFigures/ROBUSTDISAGG-TABLE-A12
cap erase $table.xml
cap erase $table.txt

foreach y in end_war_prio_bb {
	
	* -----CONDITIONAL LOGIT (won't converge with country-specific time trends)----- *

		
	* -----LPM----- *
	* BASE
	cap drop sA sA1 sA2 sP sP1 sP2 sE sE1 sE2
	gen sA  = pshock_annual_npi_p
	gen sA1 = pshock_annual_npi_p1
	gen sA2 = pshock_annual_npi_p2
	gen sP  = pshock_perennial_npi_p
	gen sP1 = pshock_perennial_npi_p1
	gen sP2 = pshock_perennial_npi_p2
	gen sE  = pshock_extract_npi_p
	gen sE1 = pshock_extract_npi_p1
	gen sE2 = pshock_extract_npi_p2	
	qui xtreg `y' $X $year $country_year $IMPX $IF, cl($clu) fe 
	outFdisaglincom
	
	* no import shock
	cap drop sA sA1 sA2 sP sP1 sP2 sE sE1 sE2
	gen sA  = pshock_annual_npi_p
	gen sA1 = pshock_annual_npi_p1
	gen sA2 = pshock_annual_npi_p2
	gen sP  = pshock_perennial_npi_p
	gen sP1 = pshock_perennial_npi_p1
	gen sP2 = pshock_perennial_npi_p2
	gen sE  = pshock_extract_npi_p
	gen sE1 = pshock_extract_npi_p1
	gen sE2 = pshock_extract_npi_p2	
	qui xtreg `y' $X $year $country_year $IF, cl($clu) fe 
	outFdisaglincom
	
	* no x/gdp adjustment
	cap drop sA sA1 sA2 sP sP1 sP2 sE sE1 sE2
	gen sA  = z_rpgr_annual_npi_p
	gen sA1 = l.z_rpgr_annual_npi_p
	gen sA2 = l2.z_rpgr_annual_npi_p
	gen sP  = z_rpgr_perennial_npi_p
	gen sP1 = l.z_rpgr_perennial_npi_p
	gen sP2 = l2.z_rpgr_perennial_npi_p
	gen sE  = z_rpgr_extract_npi_p
	gen sE1 = l.z_rpgr_extract_npi_p
	gen sE2 = l2.z_rpgr_extract_npi_p
	qui xtreg `y' $X $year $country_year $IMPX $IF, cl($clu) fe 
	outFdisaglincom
	
	* w/ PRICEMAKERS
	cap drop sA sA1 sA2 sP sP1 sP2 sE sE1 sE2
	gen sA  = pshock_annual_npi
	gen sA1 = l.pshock_annual_npi
	gen sA2 = l2.pshock_annual_npi
	gen sP  = pshock_perennial_npi
	gen sP1 = l.pshock_perennial_npi
	gen sP2 = l2.pshock_perennial_npi
	gen sE  = pshock_extract_npi
	gen sE1 = l.pshock_extract_npi
	gen sE2 = l2.pshock_extract_npi
	qui xtreg `y' $X $year $country_year $IMPX $IF, cl($clu) fe 
	outFdisaglincom
	
	* 3% pricemaker cutoff
	cap drop sA sA1 sA2 sP sP1 sP2 sE sE1 sE2
	gen sA  = pshock_annual_npi_p_03
	gen sA1 = l.pshock_annual_npi_p_03
	gen sA2 = l2.pshock_annual_npi_p_03
	gen sP  = pshock_perennial_npi_p_03
	gen sP1 = l.pshock_perennial_npi_p_03
	gen sP2 = l2.pshock_perennial_npi_p_03
	gen sE  = pshock_extract_npi_p_03
	gen sE1 = l.pshock_extract_npi_p_03
	gen sE2 = l2.pshock_extract_npi_p_03
	qui xtreg `y' $X $year $country_year $IMPX $IF, cl($clu) fe 
	outFdisaglincom
	
	* 20% pricemaker cutoff
	cap drop sA sA1 sA2 sP sP1 sP2 sE sE1 sE2
	gen sA  = pshock_annual_npi_p_2
	gen sA1 = l.pshock_annual_npi_p_2
	gen sA2 = l2.pshock_annual_npi_p_2
	gen sP  = pshock_perennial_npi_p_2
	gen sP1 = l.pshock_perennial_npi_p_2
	gen sP2 = l2.pshock_perennial_npi_p_2
	gen sE  = pshock_extract_npi_p_2
	gen sE1 = l.pshock_extract_npi_p_2
	gen sE2 = l2.pshock_extract_npi_p_2
	qui xtreg `y' $X $year $country_year $IMPX $IF, cl($clu) fe 
	outFdisaglincom
	
	* FIXED WEIGHTS
	cap drop sA sA1 sA2 sP sP1 sP2 sE sE1 sE2
	gen sA  = pshock_annual_npf80_p
	gen sA1 = l.pshock_annual_npf80_p
	gen sA2 = l2.pshock_annual_npf80_p
	gen sP  = pshock_perennial_npf80_p
	gen sP1 = l.pshock_perennial_npf80_p
	gen sP2 = l2.pshock_perennial_npf80_p
	gen sE  = pshock_extract_npf80_p
	gen sE1 = l.pshock_extract_npf80_p
	gen sE2 = l2.pshock_extract_npf80_p
	qui xtreg `y' $X $year $country_year $IMPX $IF, cl($clu) fe 
	outFdisaglincom
	
	* CENSOR OUTLIERS
	cap drop sA sA1 sA2 sP sP1 sP2 sE sE1 sE2
	gen sA  = pshock_annual_npi_p
	gen sA1 = l.pshock_annual_npi_p
	gen sA2 = l2.pshock_annual_npi_p
	gen sP  = pshock_perennial_npi_p
	gen sP1 = l.pshock_perennial_npi_p
	gen sP2 = l2.pshock_perennial_npi_p
	gen sE  = pshock_extract_npi_p
	gen sE1 = l.pshock_extract_npi_p
	gen sE2 = l2.pshock_extract_npi_p
	cap drop keeper	
	gen keeper = .
	foreach v in sA sP sE {
		qui su `v', det
		qui replace keeper = `v'>=r(p1) & `v'<=r(p99)
		qui su `v'1, det
		qui replace keeper = `v'1>=r(p1) & `v'1<=r(p99) & keeper==1
		qui su `v'2, det
		qui replace keeper = `v'2>=r(p1) & `v'2<=r(p99) & keeper==1
	}
	qui xtreg `y' $X $year $country_year $IMPX $IF & keeper==1, cl($clu) fe 
	outFdisaglincom

	* DROPPING COUNTRY-SPECIFIC TIME TRENDS
	qui xtreg `y' $X $year $IMPX $IF, cl($clu) fe 
	outFdisaglincom
	
	* EXCLUDING TIME FEs
	qui xtreg `y' $X $country_year $IMPX $IF, cl($clu) fe 
	outFdisaglincom
	
	* DROPPING COUNTRY FEs 
	qui reg `y' $X $year $country_year $IMPX $IF, cl($clu)
	outFdisaglincom	
	
	* ABSOLUTE VALUE OF SHOCKS
	cap drop sA sA1 sA2 sP sP1 sP2 sE sE1 sE2
	gen sA  = abs(pshock_annual_npi_p)
	gen sA1 = abs(l.pshock_annual_npi_p)
	gen sA2 = abs(l2.pshock_annual_npi_p)
	gen sP  = abs(pshock_perennial_npi_p)
	gen sP1 = abs(l.pshock_perennial_npi_p)
	gen sP2 = abs(l2.pshock_perennial_npi_p)
	gen sE  = abs(pshock_extract_npi_p)
	gen sE1 = abs(l.pshock_extract_npi_p)
	gen sE2 = abs(l2.pshock_extract_npi_p)	
	qui xtreg `y' $X $year $country_year $IMPX $IF, cl($clu) fe 
	outFdisaglincom
	
}

preserve
svmat pvalANNUAL 
svmat pvalPERENNIAL 
svmat pvalEXTRACT
keep if pvalANNUAL<.
keep pval*
save TablesFigures/pval-ROBUSTDISAGG-TABLE-A12,replace
restore	
	

*---------------------------------------------------------------------------------
// APPENDIX FIGURE 1: UNIT ROOTS
*---------------------------------------------------------------------------------

	preserve
	
	// individual prices
	
	mat drop _all
	
	use Data/pricedata, clear
	
	qui do Code/renamePrices
	foreach v of varlist p_* {
		replace `v' = ln(`v'/cpi2000)
	}
	
	local extract "oil gas iron gold copper phosphate alum nickel zinc lead tin manganese uranium asbestos silver coal diamond"
	local annual "sugar tobacco pepper barley soy soymeal soyoil gnut g_oil cashew copra coc_oil pal linoil snoil olvoil orange fruit ssal dairy rice wheat sorghum mz cotton jute shrimp fish fishm hide beef meat cattle sheep poultry lv_poul lvestock swine lv_swine lamb wool"
	local perennial = "coffee cocoa tea banana rubber lumber pulp"
	dis "`annual'"
	
	tsset year
	
	foreach k in `extract' `annual' `perennial' {
		qui pperron p_`k', lags(4) trend
		mat pvalUnitRoot_pperron = (nullmat(pvalUnitRoot_pperron),`=r(pval)')
		qui dfuller p_`k', lags(4) trend
		mat pvalUnitRoot_dfuller = (nullmat(pvalUnitRoot_dfuller),`=r(p)')
	
		mat pvalPP = pvalUnitRoot_pperron'
		mat pvalDF = pvalUnitRoot_dfuller'
	}
	
	svmat pvalPP
	svmat pvalDF
	
	gen commod_type = ""
	lab def ct 0 "xx"
	local i=1
	foreach v in `extract' `annual' `perennial' {
		if `i'<=65 replace commod_type = "`v'" in `i'
		lab def ct `i' "`v'", add
		local i = `i' + 1
	}
	
	sort pvalPP
	gen commod_typeN = _n
	
	lab values commod_typeN ct
	
	ren pvalPP1 pvalPP
	ren pvalDF1 pvalDF
	
	lab def ctt 0 "xx"
	forvalues j=1/65 {
		lab def ctt `j' "`=commod_type[`j']'", add
	}
	lab values commod_typeN ctt
	
	graph hbar (min) pvalPP if _n<=65,over(commod_typeN,descending label(labsize(zero) labcolor(white))) ///
		graphregion(icolor(white)) plotregion(color(white)) ylabel(,nogrid) ///
		ytitle("p-value, H{sub:0}: commodity price has a unit root (Phillips-Perron test)") blabel(group, size(tiny)) yline(0.1,lpattern(dash) lcolor(gs10))
	
	graph export TablesFigures/unitRoot_worldPriceIndex.eps, replace
	graph export TablesFigures/unitRoot_worldPriceIndex.eps, replace
	
	restore
	
	
